I've created a XYChart with numerical values different (for example temperatue with pressure) so I want to draw my own axeS just beside my chart. To do the following I've to unshow the YAxis, how should I do that ?
By using a trick: The Chart needs the Y Axis to remain in place so it knows where to render your content. You can, however, hide it. Hide the tick labels and set the axis' opacity to 0 using this code:
chart.getYAxis().setTickLabelsVisible(false);
chart.getYAxis().setOpacity(0);
The axis will still be there, but not shown.
I found that if I hid the chart using the following code:
chart.getXAxis().setTickLabelsVisible(false);
chart.getXAxis().setTickMarkVisible(false);
((Path)chart.getXAxis().lookup(".axis-minor-tick-mark")).setVisible(false);
Then I get about ~10 pixels less blank space on the bottom. IF the space was an issue for your application then you could use css offsets to correct it. This solution may have more predictable offsets.
SOLVED: I got this to work for sharing a common x-axis for two charts stacked vertically:
Create two charts, each with their own identical copy of the x-axis object, setting identical upper and lower bounds (optionally by binding).
Then hide the x-axis in the second chart like this:
chart = new LineChart<Number,Number>(xaxis2,yaxis2) {
{// hide xAxis in constructor, since not public
getChartChildren().remove(getXAxis());
// not getPlotChildren()
}
};
You'll want to set the widths of your y-axes to be the identical, e.g.
int w = 60;
yaxis.setMaxWidth(w);
yaxis.setMinWidth(w);
yaxis.setPrefWidth(w);
yaxis2.setMaxWidth(w);
yaxis2.setMinWidth(w);
yaxis2.setPrefWidth(w);
Related
So I am using MPAndroidChart as library for the viewing of graphs in my app. Now I wanted to make a LineGraph with several lines (n). The problem is that those lines do not have the same x-axis labels and I have not found a solution to put the x-axis labels in relation to the entries. Furthermore does the new line start at x-value 0. Therefore my lines do not fill the entire diagram area. Because when I have two lines with 6 entries each, the x-axis labels have the size 12. And therefore the lines end at half of the diagram.
How do I solve that?
Example LineGraph
I assume you mean the library available here: https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java
From the code I grasped, LineChart does not support dual x-axis (while it support dual y-axis, left and right). See here , XAxis.java and YAxis.java
On the other hand, looks like the x-axis fits the data range automatically (there is no method to set min/max of x-axis).
An example of the graph >>
I would suggest you to re-organize your chart to switch x and y axis, and use two y axis if your series have different range of y values, but you want to show them in a chart. Double y-axis is more common than double x-axis from my experience, and easier to understand as well.
Can I change the maximum value of Axis if LineChart is drawn already?
I know about setUpperBound() method, but I see that it works well when it is used with initialization of chart. But I want to change upper and lower bounds of an existed chart (there is no result).
The problem was in auto ranging.
Desicion:
yAxis.setAutoRanging(false);
Just take a look at this image:
I have 60 entries on the X-axis, the labels of the values do not fit in the small space so they are shown as "...". Is there a way to somehow hide some of these labels in a way that the entire label of the remaining values are shown?
You can override the tick units used by the axis by use the axis' setTickunit() method.
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/axis/NumberAxis.html#setTickUnit-org.jfree.chart.axis.NumberTickUnit-
The left three lines of data: 0.9,2450,0.4867 in the Y-axis is shown in proportion.
Right three lines (the other two data is too small to show up), how to do the left?
I see several alternatives:
Condition your data to show relative change, as is done in the chart on the left.
Add a second axis to show the larger dynamic range, as is done in DualAxisDemo2.
Enable the zoom feature in ChartPanel, as suggested in this example.
I need to add break line in some legend in JFree Chart.
I have some legends with 316 characters and need to break every 80.
Finally, I'll have 4 lines.
Anyway, I tried with "\n", "\u2424" and "
". It did nothing.
(From http://www.jfree.org/forum/viewtopic.php?f=3&t=10226 & http://www.jfree.org/forum/viewtopic.php?f=3&t=22417)
The only solution I could find (but I wished it could be avoided, since I want it to be dynamically done) is to fix a width for each legend, so it should break as I need to.
Edit : that even didn't work.
I'm using jFree Chart 0.9.20
EDIT
For the moment, with a small legend, that's what I have :
It's fine but when I have my long legends :
For that last picture, I logged my legend and break lines are here, but they don't show up with jFree Chart.
Two alternatives to consider: Given an abbreviated legend display string,
Use setLegendItemToolTipGenerator() to display the full, unbroken string as a tool tip.
renderer.setLegendItemToolTipGenerator(
new StandardXYSeriesLabelGenerator("Legend {0}"));
Use addChartMouseListener(), shown here, and forward mouse moved events over the legend to an adjacent text component.
Alright, I made it work as my client wanted.
First, you need to make a new kind of Legend, for example named MyLegend (but please, don't name it like that in the real world).
That class needs to extend Legend and implement Serializable, the same way StandardLegend does.
To be honest, I even copied/pasted the whole StandardLegend in MyLegend.
Then, you can modify the standard legend to your custom one.
For my needs, I changed :
draw() for the height and width calculation of the whole Legend group
drawSeriesElements() to split the legend's label and draw every lines one under another.
// Multi line management for Legend
String[] multiline = item.getItem().getLabel().split(System.getProperty("line.separator"));
for(int j = 0; j<multiline.length; j++) {
RefineryUtilities.drawAlignedString(multiline[j], g2,
(float) item.getLabelPosition().getX(), (float) item
.getLabelPosition().getY() + g2.getFontMetrics().getHeight()*j, TextAnchor.CENTER_LEFT);
}
createDrawableLegendItem() to calculate each item width and height.
Since, now legends are multiline, each line of one item doesn't have the same width than others. We need to find the longest one to define the item's real width.
Same goes for height. Now it's multiline, so it needs to calculate how many lines it got to know the item's real height.
Optionally, you could change drawLegendTitle() to make it multiline too.
When that class is configured as you want to, you need to apply it on your chart.
So, you do as usual :
JFreeChart chart = new JFreeChart(...);
chart.set ... // apply your series and options
MyLegend legend = new MyLegend();
legend.set... // apply your legend options if applicable
chart.setLegend(legend);
That's it.
Result :