Y axis range in XYChart - javafx-2

I have a scaling problem with XYChart plotting: my data serie ranges in float values from 0.4 and 0.5 and when plotted on a scene I get as minimum Y value 1, so I can't see anything plotted.
This problem does not arise if I use data serie with higher values such as > 100
How can I set y axis according to data serie value used?
Thanks

As stated in JavaDoc for NumberAxis constructor: public NumberAxis(double lowerBound, double upperBound, double tickUnit) -- you can provide bottom and lower bounds for your axis.
NumberAxis xAxis = new NumberAxis("X-Axis", 0d, 1d, .05);
NumberAxis yAxis = new NumberAxis("Y-Axis", 0d, 1d, .05);
ObservableList<XYChart.Series> data = FXCollections.observableArrayList(
new ScatterChart.Series("Series 1", FXCollections.<ScatterChart.Data>observableArrayList(
new XYChart.Data(.1, .1),
new XYChart.Data(.2, .2))));
ScatterChart chart = new ScatterChart(xAxis, yAxis, data);
root.getChildren().add(chart);
Update: Autoranging of the chart works for me as well, see next code and screenshot. You may want to upgrade to JavaFX 2.1 or newer.
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
ObservableList<XYChart.Series> data = FXCollections.observableArrayList(
new ScatterChart.Series("Series 1", FXCollections.<ScatterChart.Data>observableArrayList(
new XYChart.Data(.01, .1),
new XYChart.Data(.1, .11),
new XYChart.Data(.12, .12),
new XYChart.Data(.18, .15),
new XYChart.Data(.2, .2))));
XYChart chart = new LineChart(xAxis, yAxis, data);
root.getChildren().add(chart);

Related

How to draw a 2-line time series with POI 4.0.0?

Can anyone share working code that draws a timeseries (e.g x=dates,y=numeric values) chart with POI 4.0.0? I am trying to draw a 2-line chart that share dates on the X axis with POI 4.0.0
but this code:
...
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
XSSFChart chart = drawing.createChart(anchor);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
// Use a category axis for the bottom axis.
XDDFCategoryAxis bottomAxis =
chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
int chartStartRow = SHEET_START_ROW + 1;
int chartEndRow = chartStartRow + mergedData.size() - 1;
// This is selecting data from a column of dates
XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(chartStartRow, chartEndRow, 0, 0));
// This is time series 1
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(chartStartRow, chartEndRow, 1, 1));
// This is time series 2
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(chartStartRow, chartEndRow, 2, 2));
XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
data.addSeries(xs, ys1);
data.addSeries(xs, ys2);
chart.plot(data);
results in exception:
Caused by: java.lang.NullPointerException: null
at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.fillStringCache(XDDFChartData.java:260) ~[poi-ooxml-4.0.0.jar:4.0.0]
at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.plot(XDDFChartData.java:153) ~[poi-ooxml-4.0.0.jar:4.0.0]
at org.apache.poi.xddf.usermodel.chart.XDDFChart.plot(XDDFChart.java:287) ~[poi-ooxml-4.0.0.jar:4.0.0]
I would even appreciate code snippets for older POI versions, as I suspect POI 4.0.0 is buggy. Thanks
Alex's comment to treat dates as number is correct, but POI 4.0.0 charts are not ready yet and you need to apply the code hacks from here:
Problem running official examples LineChars and ScatterChart with Apache POI 4.0

How to flip axes and show it zoomed in for LineGraph MPAndroidchart

I'm not sure how to flip the axes of my chart. Below is the code I'm using.
Please see also the picture on how it looks right now. Instead of drawing the data vertically I'd like to show it horizontally. It suppose to look like a stock chart because that is what it actually is. I'm using YQL to get the historical data of the symbol as Json format.
I also see the data is scooped in the whole screen. I'd like to see the last month for example and just allow the user to swipe to the right for more or just to zoom out.
I'd appreciate some help.
float vals=0;
String[] resultStrs = new String[StockHistoryArray.length()];
for (int i = 0; i < StockHistoryArray.length(); i++) {
JSONObject chartDataObj = StockHistoryArray.getJSONObject(i);
yVals.add(new Entry(vals,(int) Float.parseFloat(chartDataObj.getString("Adj_Close")),i+1));
xVals.add(i, String.valueOf(vals));
vals++;
}
LineDataSet setting = new LineDataSet(yVals, "Stock Chart");
ArrayList<ILineDataSet> dataSets = new
ArrayList<ILineDataSet>();
dataSets.add(setting);
LineData data = new LineData(xVals, dataSets);
lineChart.setData(data);
Legend l = lineChart.getLegend();
l.setForm(Legend.LegendForm.LINE);
l.setTextColor(Color.WHITE);
XAxis x1 = lineChart.getXAxis();
x1.setTextColor(Color.WHITE);
x1.setDrawGridLines(false);
x1.setAvoidFirstLastClipping(true);
YAxis y1 = lineChart.getAxisLeft();
y1.setTextColor(Color.WHITE);
y1.setAxisMaxValue(120f);
y1.setDrawGridLines(true);
return null;
Here is the screen shot after I run this code. It is one year history.
screen capture
You are doing wrong in the following line.
yVals.add(new Entry(vals,(int) Float.parseFloat(chartDataObj.getString("Adj_Close")),i+1));
Use like this to draw the chart correctly
yVals.add(new Entry(entryVal,entryXIndex);
If your value in chartDataObj.getString("Adj_Close")
then you need to add like this
yVals.add(new Entry(Float.parseFloat(chartDataObj.getString("Adj_Close")), i);

Primefaces LineChart: Cannot change DateAxis format

LineChartModel model = new LineChartModel();
model.getAxis(AxisType.X).setTickFormat("%H:%#M:%S");
DateAxis dateAxis = new DateAxis("Time");
model.getAxes().put(AxisType.X, dateAxis);
Even I set my tick format as above it renders the following x axis labels (adds month info unexpectedly):
To render month info is not desired situation by my clients.
You are creating a new Axis and setting it as the X axis after setting the tick format for what becomes the old Axis so those changes will be wiped out.
Try either of the following code snippets.
LineChartModel model = new LineChartModel();
DateAxis dateAxis = new DateAxis("Time");
model.getAxes().put(AxisType.X, dateAxis);
model.getAxis(AxisType.X).setTickFormat("%H:%#M:%S");
Or
LineChartModel model = new LineChartModel();
DateAxis dateAxis = new DateAxis("Time");
datAxis.setTickFormat("%H:%#M:%S");
model.getAxes().put(AxisType.X, dateAxis);

JavaFx logarithmic Axis in BarChart

I need a logarithmic y-axis in a JavaFx BarChart. I tried use this
implementation of a valueAxis .
That code seems to be correct and it works in a lineChart as a x-axis.
However it works not in a BarChart, there are simply no bars in the plot. The valueAxis should work in any kind of chart, so I think the BarChart is the problem. But I can not find the bug.
Here is my code, how I tested the chart:
LogarithmicAxis yAxis = new LogarithmicAxis();
yAxis.setMinorTickVisible(true);
yAxis.setVisible(true);
yAxis.setLabel("label");
chart = new BarChart<String, Number>(xAxis, yAxis);
chart.setAnimated(false);
Series<String, Number> aSeries = new Series<String, Number>();
aSeries.getData().add(new XYChart.Data("test1", Math.log(90)));
aSeries.getData().add(new XYChart.Data("test2", Math.log(100)));
answer = FXCollections.observableArrayList();
answer.addAll(aSeries);
chart.getData().addAll(answer);
chartFxPanel.setScene(new Scene(chart));
chart.layout();

Fix Bar Chart Width and Spacing between bars in JFreeChart

I have stacked bar chart in which the number of columns is dynamic, can change from 1 to n columns. I want the spacing between the charts and width of the bar to be consistent. How do I fix it. Please suggest solutions / ideas.
In a Stacked Bar chart, you can change the spacing between bars using
CategoryAxis.setLowerMargin
CategoryAxis.setMargin and
CategoryAxis.setUpperMargin
Code is below
protected JFreeChart generateGraph() {
CategoryAxis categoryAxis = new CategoryAxis("Categories");
categoryAxis.setLowerMargin(.01);
categoryAxis.setCategoryMargin(.01);
categoryAxis.setUpperMargin(.01);
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
ValueAxis valueAxis = new NumberAxis("Values");
StackedBarRenderer renderer = new StackedBarRenderer();
renderer.setBarPainter(new StandardBarPainter());
renderer.setDrawBarOutline(false);
renderer.setShadowVisible(false);
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
CategoryPlot plot = new CategoryPlot( _dataset,
categoryAxis,
valueAxis,
renderer);
plot.setOrientation(PlotOrientation.VERTICAL);
JFreeChart chart = new JFreeChart( "Title",
JFreeChart.DEFAULT_TITLE_FONT,
plot,
true);
//ChartFactory.getChartTheme().apply(_chart);
return chart;
}
StackedBarRenderer devotes some effort to making the "spacing between the [bars] and width of the bar to be consistent." It's not clear what you want it to do differently as the number of columns changes. The relevant geometry is determined by the parent BarRenderer in such methods as calculateBarWidth(), which can be overridden as desired. Also, verify that there is a value for each category in each series.

Resources