Jfree chart xy line implementation - java

I want to plot 4 lines in one dataset but I cannot. Could any1 tell me what libs in need in jfree and how I can implement them in 1 chart
public void visualise(){
//visualise data
XYSeries series = new XYSeries("Membrane mVolt");
XYSeries series2 = new XYSeries("potassium_channel_n_gate_n");
XYSeries series3 = new XYSeries("sodium_channel_h_gate_h");
XYSeries series4 = new XYSeries("sodium_channel_m_gate_m");
//add data to charts
for(int l=0;l<301;l++){
series.add(tData[l], YData[0][l]);
series2.add(tData[l], YData[1][l]);
series3.add(tData[l], YData[2][l]);
series4.add(tData[l], YData[3][l]);
}
//create lines
XYDataset xyDataset = new XYSeriesCollection(series);
XYDataset xyDataset2 = new XYSeriesCollection(series2);
XYDataset xyDataset3 = new XYSeriesCollection(series3);
XYDataset xyDataset4 = new XYSeriesCollection(series4);
//visualize
JFreeChart chart = ChartFactory.createXYLineChart("Membrane", "time","data",xyDataset, PlotOrientation.VERTICAL, true, true, false);
JFreeChart chart2 = ChartFactory.createXYLineChart("Potassium Channel n", "time""data",xyDataset2,PlotOrientation.VERTICAL, true, true, false);
JFreeChart chart3 = ChartFactory.createXYLineChart("Sodium Channel h", "time","data",xyDataset3, PlotOrientation.VERTICAL, true, true, false);
JFreeChart chart4 = ChartFactory.createXYLineChart("Sodium Channel m", "time", "data",xyDataset4,
PlotOrientation.VERTICAL, true, true, false);
//open window
ChartFrame frame1=new ChartFrame(" ",chart);
ChartFrame frame2=new ChartFrame(" ",chart2);
ChartFrame frame3=new ChartFrame(" ",chart3);
ChartFrame frame4=new ChartFrame(" ",chart4);
//make window
frame1.setVisible(true);
frame1.setSize(900,900);
frame2.setVisible(true);
frame2.setSize(900,900);
frame3.setVisible(true);
frame3.setSize(900,900);
frame4.setVisible(true);
frame4.setSize(900,900);
}

This example shows how to add multiple series to a DefaultXYDataset.

Related

Issues creating a jfreechart score bar/chart

I am trying to create a score/percent/status bar using jfreechart.
The image show what it's suppose to look like.Good ScoreChart
It looks like one bar from a stack chart, so I try to adjust from there. (Do let me know if there is a better way to do this other than the stack chart)
I am seeing multiple issues doing the bar this way. I can't make the start line and end line longer. I can't put text on top of both start/end line.(I think they get chopped off) I cannot put center text in the middle of the bar either...
Here is what I got now. Bad ScoreChart
Here is the code for the bad Chart.
public JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createStackedBarChart(null, null, null, dataset,
PlotOrientation.HORIZONTAL, false, false, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();
((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
plot.getRenderer().setSeriesPaint(0, DONUT_CHART_ORANGE);
plot.getRenderer().setSeriesPaint(1, DONUT_CHART_BLUE);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setVisible(false);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(false);
plot.setRangeGridlinesVisible(false);
plot.setBackgroundPaint(Color.WHITE);
plot.setOutlineVisible(false);
CategoryItemRenderer renderer = ((CategoryPlot) chart.getPlot()).getRenderer();
//renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelGenerator(new CustomItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER);
renderer.setBasePositiveItemLabelPosition(position);
Marker startMarker = new ValueMarker(0);
startMarker.setPaint(Color.black);
startMarker.setLabel("000000000");
startMarker.setStroke(new BasicStroke(1.0f));
startMarker.setLabelOffset( new RectangleInsets(UnitType.ABSOLUTE, 10,0,0,0));
startMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT) ;
startMarker.setLabelTextAnchor(TextAnchor.BASELINE_CENTER);
plot.addRangeMarker(startMarker);
Marker endMarker = new ValueMarker(1000);
endMarker.setPaint(Color.black);
endMarker.setLabel("1000");
endMarker.setStroke(new BasicStroke(1.0f));
endMarker.setLabelAnchor(RectangleAnchor.TOP);
endMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
plot.addRangeMarker(endMarker);
RectangleInsets chartRectangle = new RectangleInsets(35, 0, 35, 0);
chart.setPadding(chartRectangle);
return chart;
}
public CategoryDataset createDataset() {
double inValue = 775;
double[][] data = new double[][] {
{ inValue },
{ 1000- inValue },
};
return DatasetUtilities.createCategoryDataset("A", "B", data);
}
Any Idea how to move the text so they show up on top of the lines and at the center of the bar?
Can we adjust the line width at the beginning and the end?
Thanks for any idea/info.

Creating normal distribution graph with multiple lines using JFreechart

I want to create a normal distribution graph, with more than one line on it.
Exactly like this one:
I searched the internet but only found some guide to common lines graph, not normal distribution graph.
I can't figure out how to do this, please help
Below is part of my code
public ChartPanel getPanelNormalWeightAndSpecies() {
double mean = getMean();
double std = getStd(mean);
Function2D normal = new NormalDistributionFunction2D(mean, std);
Function2D normal2 = new NormalDistributionFunction2D(0.0, 1.0);
XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, 50, 150, 100, "Normal");
XYDataset dataset2 = DatasetUtilities.sampleFunction2D(normal2, 50, 150, 100, "Normal2");
JFreeChart chart = ChartFactory.createXYLineChart(
"Test",
"X",
"Y",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
XYPlot plot = chart.getXYPlot();
XYItemRenderer xyitem = plot.getRenderer();
plot.setDataset(1, dataset2);
plot.setRenderer(1, xyitem);
ChartPanel chartPanel = new ChartPanel(chart);
return chartPanel;
}
Your code is on track, there is an example of how to do exactly what you want in the JFreeChart Samples and you can get the source code by buying the developer guide

JFreechart XYAreaChart changing color

I have an area chart , I want to color the area of the chart which is >0 in green and the area which is <0 in red. I tried the SetSeriesPaint() method but it makes all the area chart colored by one color. How to do this?
Here's my code:
final XYSeries series = new XYSeries("Data");
XYSeriesCollection dataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYAreaChart("Fun Meter", "", "",
dataset, PlotOrientation.VERTICAL, false, false, false);
// jframe=new ChartFrame("Fun Meter", chart);
ChartPanel CP = new ChartPanel(chart);
XYPlot xyPlot = (XYPlot) chart.getPlot();
xyPlot.getRenderer().setSeriesPaint(0, Color.red);
xyPlot.setForegroundAlpha(0.75f);
Use a XYDifferenceRenderer:
XYDifferenceRenderer r = new XYDifferenceRenderer(Color.green,Color.red, false);
plot.setRenderer(r);

JFreeChart Large Data Can't Read Axis

I am using JFreeChart to plot a line graph. The app reads in sensory data every 100 milliseconds so for a few minutes of capture it's a a lot of data. I don't plot the graph dynamically, it is static. I am using a Category plot since the axis can sometimes be decimal values, other times it can be strings, other times it can be boolean. My issue is the X axis (time) has so much data I can't make out the text:
Anyone know what I can do here to? Any tips or tricks to deal with this will be great!
private CategoryDataset createDataset() {
String series1 = "First";
String series2 = "Second";
String category1 = "Category 1";
String category2 = "Category 2";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < time.size(); i++) {
dataset.addValue(Math.random(), series1, time.get(i));
}
return dataset;
}
private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createLineChart(
"Line Chart Demo 6", // chart title
"Time", // x axis label
"RPM", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
plot.setRenderer(renderer);
return chart;
}
public void setLists(ArrayList<String> time) {
this.time = time;
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
You can turn off the Lables and TickMarks by adding:
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis cx = new CategoryAxis();
cx.setTickLabelsVisible(false);
cx.setTickMarksVisible(false);
plot.setDomainAxis(cx);
If you wantto display a subset of the labels (every nth value) then you will need to subclass CategoryAxis so you can overide CategoryAxis#drawCategoryLabels() andCategoryAxis#drawTickMarks()

how to set first and last tick mark and x axis value in jfree bar chart x-axis

im using jfree chart in my application. For my application i need to mark only first and last value of the x axis and also tick mark.
I've tried
String Male1 = "First";
String Male2 = "sec";
String Female1 = "0-4";
String Female2 = "5-18";
String Female3 = "19-45";
String Female4 = "46-64";
String Female5 = "65+";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(202, Male1, Female1);
dataset.addValue(130, Male2, Female1);
dataset.addValue(216, Male1, Female2);
dataset.addValue(0, Male2, Female2);
dataset.addValue(248, Male1, Female3);
dataset.addValue(458, Male2, Female3);
dataset.addValue(517, Male1, Female4);
dataset.addValue(623, Male2, Female4);
dataset.addValue(1481, Male1, Female5);
dataset.addValue(680, Male2, Female5);
final JFreeChart chart = ChartFactory.createBarChart(
"", "", "", dataset,
PlotOrientation.HORIZONTAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setOutlineVisible(false);
plot.setRangeGridlinesVisible(false);
plot.setRangeGridlineStroke(new BasicStroke(0.2f));
plot.setAxisOffset(RectangleInsets.ZERO_INSETS);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setAxisLinePaint(Color.decode("#C1C1C1"));
rangeAxis.setAxisLineVisible(true);
rangeAxis.setTickMarksVisible(false);
rangeAxis.setTickMarkOutsideLength(0f);
final CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
categoryAxis.setTickMarksVisible(false);
categoryAxis.setAxisLineVisible(false);
BarRenderer br = new BarRenderer();
br.setItemMargin(0.03);
br.setShadowVisible(false);
br.setBarPainter(new StandardBarPainter());
br.setSeriesPaint(0, Color.decode("#999999"));
br.setSeriesPaint(1, Color.decode("#CCCCCC"));
chart.getCategoryPlot().setRenderer(br);
chart.removeLegend();
try {
ChartUtilities.saveChartAsPNG(new File("/media/hari/668ea9a3-d26c-4896-a2f0-756dfb532756/jfreeBarchart.png"), chart, 280, 180);
System.out.println("=====Bar chart=====");
} catch (Exception e) {
e.printStackTrace();
}
For the above code im getting
But my expectation is
Please help me to get the expected chart in jfree bar chart
If I understood correctly you want to only display two ticks (0 and 1500) and add a noticeable inside tick mark to both.
To display only two ticks you can call setTickUnit with the appropriate size argument. This size will be used by the axis object to generate the visible ticks.
To add tick marks on the inside you can call setTickMarkInsideLength.
So adding these three lines should do the trick:
rangeAxis.setTickUnit(new NumberTickUnit(1500));
rangeAxis.setTickMarksVisible(true);
rangeAxis.setTickMarkInsideLength(3f);

Categories

Resources