Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xyseries to generic1 #314

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ public static Method getMethod(
String name = pre + lowerCaseName;
method = methodMap.get(name);
if (method != null) {
if (method.getParameterCount() != (read ? 0 : 1)) {
if (method.getParameterTypes().length != (read ? 0 : 1)) {
method = null;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries;
import org.knowm.xchart.XcTrans3;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.demo.charts.RealtimeExampleChart;
import org.knowm.xchart.internal.series.Series.DataType;
import org.knowm.xchart.style.Styler.ChartTheme;

/**
Expand All @@ -24,7 +27,8 @@ public class RealtimeChart01 implements ExampleChart<XYChart>, RealtimeExampleCh

private XYChart xyChart;

private List<Double> yData;
private List<Double> yData = getRandomData(5);

public static final String SERIES_NAME = "series1";

public static void main(String[] args) {
Expand Down Expand Up @@ -67,7 +71,7 @@ public void run() {
@Override
public XYChart getChart() {

yData = getRandomData(5);
//yData = getRandomData(5);

// Create Chart
xyChart =
Expand All @@ -77,31 +81,49 @@ public XYChart getChart() {
.theme(ChartTheme.Matlab)
.title("Real-time XY Chart")
.build();
xyChart.addSeries(SERIES_NAME, null, yData);

xyChart.addSeries(SERIES_NAME, yData
, new XcTrans3<List<?>, Integer, Object , Number>() {
@Override
public Number trans(List<?> o1, Integer o2, Object o3) {
return o2;
}
}
, new XcTrans3<List<?>, Integer, Object, Number>() {
@Override
public Number trans(List<?> o1, Integer o2, Object o3) {
return yData.get(o2);
}
}
, null
, DataType.Number
);

return xyChart;
}


public void updateData() {

// Get some new data
List<Double> newData = getRandomData(1);

yData.addAll(newData);
yData.add(nextRandValue());

// Limit the total number of points
while (yData.size() > 20) {
yData.remove(0);
}

xyChart.updateXYSeries(SERIES_NAME, null, yData, null);
xyChart.updateXYSeries(SERIES_NAME);
}

private List<Double> getRandomData(int numPoints) {
static double nextRandValue() {
return Math.random() * 100;
}
static List<Double> getRandomData(int numPoints) {

List<Double> data = new CopyOnWriteArrayList<Double>();
for (int i = 0; i < numPoints; i++) {
data.add(Math.random() * 100);
data.add(nextRandValue());
}
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public void updateData() {
errorBars.add(20 * Math.random());
errorBars.remove(0);

xyChart.updateXYSeries(SERIES_NAME, xData, yData, errorBars);
xyChart.updateXYSeries(SERIES_NAME
//, xData, yData, errorBars
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public void updateData() {
while (bubbleData.size() > 20) {
bubbleData.remove(0);
}
bubbleChart.updateBubbleSeries(SERIES_NAME, null, yData, bubbleData);
bubbleChart.updateBubbleSeries(SERIES_NAME
//, null, yData, bubbleData
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void main(String[] args) {
new SwingWrapper(chart).displayChart();
}

private static List<Double> getGaussian(int number, double mean, double std) {
public static List<Double> getGaussian(int number, double mean, double std) {

List<Double> seriesData = new LinkedList<Double>();
for (int i = 0; i < number; i++) {
Expand Down
46 changes: 25 additions & 21 deletions xchart/src/main/java/org/knowm/xchart/BubbleChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,19 @@ public BubbleSeries addSeries(
* @param newBubbleData - set null if there are no error bars
* @return
*/
public BubbleSeries updateBubbleSeries(
String seriesName,
List<?> newXData,
List<? extends Number> newYData,
List<? extends Number> newBubbleData) {

return updateBubbleSeries(
seriesName,
Utils.getDoubleArrayFromNumberList(newXData),
Utils.getDoubleArrayFromNumberList(newYData),
Utils.getDoubleArrayFromNumberList(newBubbleData));
}
// public BubbleSeries updateBubbleSeries(
// String seriesName
// //, List<?> newXData,
// //List<? extends Number> newYData,
// //List<? extends Number> newBubbleData
// ) {
//
// return updateBubbleSeries(
// seriesName,
// Utils.getDoubleArrayFromNumberList(newXData),
// Utils.getDoubleArrayFromNumberList(newYData),
// Utils.getDoubleArrayFromNumberList(newBubbleData));
// }

/**
* Update a series by updating the X-Axis, Y-Axis and bubble data
Expand All @@ -160,19 +161,22 @@ public BubbleSeries updateBubbleSeries(
* @return
*/
public BubbleSeries updateBubbleSeries(
String seriesName, double[] newXData, double[] newYData, double[] newBubbleData) {
String seriesName
//, double[] newXData, double[] newYData, double[] newBubbleData
) {

Map<String, BubbleSeries> seriesMap = getSeriesMap();
BubbleSeries series = seriesMap.get(seriesName);
if (series == null) {
if (series == null)
throw new IllegalArgumentException("Series name >" + seriesName + "< not found!!!");
}
if (newXData == null) {
double[] generatedXData = Utils.getGeneratedDataAsArray(newYData.length);
series.replaceData(generatedXData, newYData, newBubbleData);
} else {
series.replaceData(newXData, newYData, newBubbleData);
}

// if (newXData == null) {
// double[] generatedXData = Utils.getGeneratedDataAsArray(newYData.length);
// series.replaceData(generatedXData, newYData, newBubbleData);
// } else {
// series.replaceData(newXData, newYData, newBubbleData);
// }
series.replaceData();

return series;
}
Expand Down
74 changes: 69 additions & 5 deletions xchart/src/main/java/org/knowm/xchart/BubbleSeries.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
package org.knowm.xchart;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;

import org.knowm.xchart.internal.chartpart.RenderableSeries;
import org.knowm.xchart.internal.chartpart.RenderableSeries.LegendRenderType;
import org.knowm.xchart.internal.series.NoMarkersSeries;
import org.knowm.xchart.style.Styler.LegendPosition;
import org.knowm.xchart.style.markers.SeriesMarkers;

/**
* A Series containing X, Y and bubble size data to be plotted on a Chart
*
* @author timmolter
*/
public class BubbleSeries extends NoMarkersSeries {
public class BubbleSeries
//extends NoMarkersSeries // not needed anymore
extends XYSeries
{
public static List<Double> getGaussian(int number, double mean, double std) {
Random random=new Random();

List<Double> seriesData = new LinkedList<Double>();
for (int i = 0; i < number; i++) {
seriesData.add(mean + std * random.nextGaussian());
}

return seriesData;
}
public static void main(String[] args) {
JFrame fr=new JFrame(BubbleSeries.class.getSimpleName());

XYChart chart;
{
chart = new XYChartBuilder()
//.width(600).height(500)
.title("Gaussian Blobs").xAxisTitle("X").yAxisTitle("Y").build();

// Customize Chart
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Scatter);
chart.getStyler().setChartTitleVisible(false);
chart.getStyler().setLegendPosition(LegendPosition.InsideSW);
chart.getStyler().setMarkerSize(16);

// Series
chart.addSeries("Gaussian Blob 1", getGaussian(1000, 1, 10), getGaussian(1000, 1, 10));
XYSeries series = chart.addSeries("Gaussian Blob 2", getGaussian(1000, 1, 10), getGaussian(1000, 0, 5));
series.setMarker(SeriesMarkers.DIAMOND);
}

XChartPanel<XYChart> panel = new XChartPanel<XYChart>(chart);
fr.getContentPane().add(panel);
fr.setSize(1920>>1, 1080>>1);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setVisible(true);

}

private BubbleSeriesRenderStyle bubbleSeriesRenderStyle = null;

Expand All @@ -21,9 +69,25 @@ public class BubbleSeries extends NoMarkersSeries {
* @param yData
* @param bubbleSizes
*/
public BubbleSeries(String name, double[] xData, double[] yData, double[] bubbleSizes) {

super(name, xData, yData, bubbleSizes, DataType.Number);
public BubbleSeries(String name
, double[] xData, double[] yData, double[] bubbleSizes
) {

super(name
, xData, yData, bubbleSizes
, DataType.Number);

System.out.println("list.sz="+list.size());
if(bubbleSizes==null)
System.out.println("bubbleSizes="+bubbleSizes);
else
System.out.println("bubbleSizes.len="+bubbleSizes.length);
System.out.println("extra?="+hasExtraValues());
}

@Override
protected void calculateMinMax() {
calculateMinMax(false);
}

public BubbleSeriesRenderStyle getBubbleSeriesRenderStyle() {
Expand Down
Loading