Primefaces LineChart: Cannot change DateAxis format - jsf

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);

Related

Why are the bars of my p:chart overlapping

Developing a Java8 application with jsf and primefaces, I encountered a small issue with the bar of my barChart which are overlapping. So my question is: How can I configure the width of the chart without impacting the interval of the axis?
My first idea was to modify the axis with xAxis.setTickInterval(); but the problem is that my axis is a DateAxis in month/year and it showed multiple times the same month in the axis (not multiple time the bar (cf the screenshot) reducing the overlapping).
And the code:
private void createChart() {
globalGradeEvolution = new BarChartModel();
globalGradeEvolution.setTitle("Evaluation globale");
globalGradeEvolution.setLegendPosition("e");
//Define two axis
Axis yAxis = globalGradeEvolution.getAxis(AxisType.Y);
DateAxis xAxis = new DateAxis("Dates");
//cacteristics of x axis
xAxis.setTickAngle(-30);
xAxis.setMax(LocalDate.now().getYear()+"-"+LocalDate.now().getMonthValue()+"-"+"15"); // middle of the month
xAxis.setTickFormat("%m/%Y"); // example: 08/2018
//cacteristics of y axis
yAxis.setLabel("Notes");
yAxis.setMin(min);
yAxis.setMax(10);
yAxis.setTickFormat("%.1f");
globalGradeEvolution.getAxes().put(AxisType.X, xAxis);
}
<h:form>
<p:chart type="bar" model="#{statChartView.globalGradeEvolution}" style="height:400px" responsive="true"/>
</h:form>
This is a bug in PrimeFaces + JqPlot combination.
The bug has been reported to PrimeFaces here: https://github.com/primefaces/primefaces/issues/3684

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);

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();

Y axis range in XYChart

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);

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