Mouse event when clicking data title in JavaFX - java

I have created a simple horizontal barchart using JavaFX in Java 1.8. This displays correctly with a bottom legend, list of paths on the left and bars showing data grouped into 3 series. I would like to open a page that is dynamically populated based on the path chosen with the path on the left used as the link but I have not been able to find a way to create a mouse event when clicking on that data text.
In the sample code below the text retrieved by my "path" class's "getpath" method is what I would like to turn into a link.
Is there anyway to do this?
I've reviewed documentation on the BarChart and XYChart classes but I cannot find a way to get the value of a given item in the chart, and even if a did I cannot set an action event because the data value doesn't appear to have a specific node to set the event on.
Doing this with a button and/or drop down works as expected but i'd like to use the text built into the chart as a link.
Most of the other similar questions I have found referenced using a label for the title but that won't work in this case as the number of items in my chart will vary on the dataset passed into it.
//method for building chart
protected BarChart getChart(ObservableList<myClass> chartData) {
//Setup a horizontal barchart for displaying heat stats.
final NumberAxis xAxis = new NumberAxis();
final CategoryAxis yAxis = new CategoryAxis();
final BarChart<Number,String> bc =
new BarChart<Number,String>(xAxis,yAxis);
bc.setTitle("");
xAxis.setLabel("Ops");
xAxis.setTickLabelRotation(90);
yAxis.setLabel("NFS Exports with lock contention");
//Create series to hold data for each event type to show
XYChart.Series series1= new XYChart.Series();
series1.setName("Series1");
XYChart.Series series2= new XYChart.Series();
series2.setName("Series2");
XYChart.Series series3= new XYChart.Series();
series3.setName("Series3");
for (myClass path: chartData)
{
series1.getData().add(new XYChart.Data(path.getData1(), path.getPath()));
series2.getData().add(new XYChart.Data(path.getData2(), path.getPath()));
series3.getData().add(new XYChart.Data(path.getData3(), path.getPath()));
}
bc.getData().addAll(series1, series2, series3);
return bc;
}
//method for getting sample data
public ObservableList<myClass> getPathData()
{
ObservableList<myClass> exports = FXCollections.observableArrayList();
paths.add(new myClass("/path1", 12.0, 30.0, 62.0));
paths.add(new myClass("/path2", 20.0, 43.0, 82.0));
paths.add(new myClass("/path3", 0.0, 18.0, 23.0));
paths.add(new myClass("/path4", 40.0, 26.0, 75.0));
paths.add(new myClass("/path5", 8.0, 4.0, 5.0));
return paths;
}
What I would like is a way to make clicking on the path run a function passing in the path that was selected.

Related

How to speed up drawing graphs (charts) in Java? (own library)

How to write a java library that would display graphs (charts) with large datasets (100-200 thousand points)? I think that such a large graph does not need to be displayed entirely, I probably only need to display a part of it, for example, a thousand points. But I need to be able to move around the graph and scale it.
It is also necessary that the charts are built in real time. As I understand it, for any action with the chart, the data must be displayed again, that is, thereby updated.
Maybe for speed it will be possible to do parallel execution of some parts of the code for drawing the chart. Because the speed of execution will be the main thing in this library.
I also plan to convert the chart to svg to be inserted into the html page.
Now I just build a graph using JavaFX, and it is very slow to build on a couple of thousand points and JavaFX does not seem to have tools for scaling.
public class FXLineChart {
private List<Long> dataset;
public FXLineChart(List<Long> dataset) {
this.dataset = dataset;
}
public void drawChart(Stage stage) {
stage.setTitle("Chart");
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
final LineChart<Number, Number> lineChart =
new LineChart<Number, Number>(xAxis, yAxis);
lineChart.setTitle("Check dataset");
XYChart.Series series = new XYChart.Series();
for (int i = 0; i < dataset.size(); i++) {
series.getData().add(new XYChart.Data(i, dataset.get(i)));
}
lineChart.getData().add(series);
Scene scene = new Scene(lineChart, 800, 600);
stage.setScene(scene);
stage.show();
}
}
JFreeChart is slightly better, but the chart slows down at several tens of thousands of points. I did not find information about real time in JFreeChart.
Maybe someone has a code for a similar task or idea? The challenge is that according to the assignment it should be its own library.

How to draw grouped bar chart in Android with different number of columns in each group?

This is the chart that I want to create (click on the link to view image):
Desired chart
I have researched a lot about Grouped Bar Chart in MPAndroidChart but that library seems to restrict. Each group must have the same number of columns and the column order must be consistent. Does anyone know how to create a grouped bar chart like in my image using MPAndroidChart (or any other libraries)?
Please help me. Thank you in advance.
Could you try this code?
List<String> xValues = ...; // "Shop1", "Shop2", "Shop3"
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new MyValueFormatter(xValues));
// create 2 datasets
BarDataSet set1 = new BarDataSet(valuesProduct1, "Product1"); //valuesProduct1 are the values corresponding to the shops,
set1.setColor(Color.BLUE);
BarDataSet set2 = new BarDataSet(valuesProduct2, "Product2");
set2.setColor(Color.RED);
BarData data = new BarData(set1, set2);
chart.setData(data);
chart.groupBars(...); // available since release v3.0.0
chart.invalidate(); // refresh
If you have some troubles where the shop has no products, just put zero on the concrete position

Graphing scientific data in LineChart from MPAndroidChart

I am using MPAndroidChart library to draw graphs (especially LineCharts) in my App.
To draw a LineChart with the mentioned library first we need to create entries and labels as follow:
// Getting LineChart
LineChart lineChart = (LineChart) rootView.findViewById(R.id.chart);
// Creating list of entry
ArrayList<Entry> entries = new ArrayList<>();
// Creating labels
ArrayList<String> labels = new ArrayList<String>();
// Fill entries and lables
entries.add(new Entry(326.422f, 0));
entries.add(new Entry(8.36f, 1));
entries.add(new Entry(6.5f, 2));
entries.add(new Entry(2.37f, 3));
entries.add(new Entry(18.13f, 4));
entries.add(new Entry(9f, 5));
labels.add("0");
labels.add("1");
labels.add("2");
labels.add("3");
labels.add("4");
labels.add("5");
// Create dataset
final LineDataSet dataset = new LineDataSet(entries, "Legend description");
// Create LineData with labels and dataset prepared previously
LineData data = new LineData(labels, dataset);
// Set the data and list of labels into chart
lineChart.setData(data);
Ok this is working, but the point is what if I want to graph a set of coordinates like this: X = {(35.3, 22.9), (69.39, 27.36), (66.37, 31.697), (58.36, 36.32), (45.336, 38.296), (25.39, 40), (67.396, 43.633)}.
The constructor of Entry accepts a float number as first parameter and an integer as second parameter, so how can I give the above X set to the LineChart?
Someone could say that I can set the labels accordingly, for example the first label could be labeled as "22.9", the second as "27.36", and so on... But this is mathematically wrong as the graph is not scaled properly.
In the documentation I found classes like Entry, BarEntry, BubbleEntry, CandleEntry but there is nothing something like LineEntry.
Can anyone point me to the right direction on how to achieve this goal?
Thank you,
HSB
Currently only integers are supported for the x-axis. The reason therefore is that each string on the x-axis should correspond to a value on the y-axis.
This will change in the next release of the library, where both values will be changed to double.
The new version should be released in April.

Display values in JFreeChart spider chart

The spider web plot of JFreeChart does not display values close to the marked points, as default:
what must be done to display the numbers?
That's what I have now, in a method returning StreamedContent (for PrimeFaces' p:graphicImage:
CategoryDataset categorydataset = createCategoryDataset(displayBy, configuration, null);
SpiderWebPlot plot = new SpiderWebPlot(categorydataset);
plot.setInteriorGap(0.40);
plot.setNoDataMessage("No data available");
plot.setStartAngle(0);
plot.setLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", NumberFormat.getInstance()));
plot.setWebFilled(true);
JFreeChart chart = new JFreeChart(title, TextTitle.DEFAULT_FONT, plot, true);
File chartFile = new File(String.valueOf(new Random().nextLong()));
ChartUtilities.saveChartAsPNG(chartFile, chart, 375, 300);
return new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
EDIT: After #trashgod's suggestion, some values appeared but no in the expected place. I want the columnKey values, not the `rowKey' values (addValue method in DefaultCategoryDataset class):
What must be done to display the numbers?
You can label points on the plot using a CategoryItemLabelGenerator, as shown here.
What matters are the three other [points].
It looks like you might be able to add a second series, one with a second rowKey, in order to get labels associated with those points.

Box plot using JFreeChart

First off, I am new to Java and to Stackoverflow. So I hope I can supply enough clarity in my question.
My goal is to create a box plot using jfreechart to keep track of measurement values from every day use. I want to do this by storing minimal amount of data ie. by storing statists of mean, standard deviation, median, 1Q,3Q, min and maximum. This should then be visualized by a box plot for each day measured.
I have looked at the box plot demo here
http://www.java2s.com/Code/Java/Chart/JFreeChartBoxAndWhiskerDemo.htm
In this demo they create the dataset and add all the values to the dataset, then adds it to the plot. The dataset itself contains methods to return the mean, median etc. of the dataset to be able to create the plot. See the code below for a snip from the demo in the link above.
DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();
//some type of algorithm to add values to the dataset
dataset.add(//values, series and type here);
// Return the finished dataset
CategoryAxis xAxis = new CategoryAxis("Type");
NumberAxis yAxis = new NumberAxis("Value");
yAxis.setAutoRangeIncludesZero(false);
BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
renderer.setFillBox(false);
renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis,
renderer);
JFreeChart chart = new JFreeChart("Box-and-Whisker Demo",
new Font("SansSerif", Font.BOLD, 14), plot, true);
So my question is, how should I do to just add the median, Q1,Q3, mean, minimum and maximum values to create the box plot? Because in the demo above they base the plot of a complete sample set.
You can create your own dataset class and use it to create the chart.
Create your own implementation of BoxAndWhiskerCategoryDataset and use it in place of DefaultBoxAndWhiskerCategoryDataset.

Categories

Resources