I am working with java, I want to draw a line chart which includes at least two lines at the same time. I can only pass one DefaultCategoryDataset each time as a parameter, and this DefaultCategoryDataset represent one line. so how can I do that?
ChartFactory.createLineChart("String", "String", "String", "DefaultCategoryDataset", "PlotOrientation.HORIZONTAL", "boolean", "boolean", "boolean");
You have to "feed" data with more than one series.
Here it is how i handle this with XYLineChart:
1) I have a private object
private XYSeriesCollection data =null;
2) It is initialised into a method and then another method that "feeds" data is called.
data = new XYSeriesCollection();
fillXYSeries();
3) Here is this method:
private void fillXYSeries(){
data.removeAllSeries();
for(int i=0;i<tables.size();i++){
final XYSeries series = new XYSeries(tables.get(i).getName());
for(int j=0;j<mostIntensiveTables.get(i).getChangesForChart().size();j++){
series.add(j,mostIntensiveTables.get(i).getChangesForChart().get(j));
}
int found=0;
for(int k=0;k<data.getSeriesCount();k++){
if(data.getSeries(k)==series){
found=1;
break;
}
}
if(found==0){
data.addSeries(series);
}
}
}
and finally i construct my chart with data object as parameter:
final JFreeChart chart = ChartFactory.createXYLineChart(
"Most Updated Tables",
"Version ID",
"Number of Changes",
data,
PlotOrientation.VERTICAL,
true,
true,
false
);
Related
I have to implement an histogram using JFreeChart API. This histogram has to represent the datas of this JTable:
So I have a JTable with three columns: "thea", "type", "Number of occurrences". My histogram has two targets: the first is to count the number of occurrences of each thea field; the second is to mark with different colors the bars corresponding to JTable records with different types.
To implement my histogram I used a DefaultCategoryDataset:
private DefaultCategoryDataset createDataset(ArrayList<String>fieldsOccs) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(int i = 0; i<this.fieldsOccs.size() && i<end; i++) {
String thea = fieldsOccs.get(i).getFieldName();
String type = fieldsOccs.get(i).getType();
int occurrences = fieldsOccs.get(i).getOccurrences();
dataset.setValue(occurrences, type, thea);
}
return dataset;
}
Anf then I create my chart using a createChart method:
private JFreeChart createChart(DefaultCategoryDataset dataset) {
JFreeChart chart = ChartFactory.createBarChart(
"",
"", //X-axis title
"", //Y-axis title
dataset, //dataset
PlotOrientation.HORIZONTAL, //plot orientation
true, //show legends
true, //use tooltips
false //generate URLs
);
return chart;
}
This is what I get:
As you can see in the picture it is not nice to see. The values on x axes are not formatted correctly.
How can I solve this rendering problem?
--edit
I have this problem just in case of more types in the JTable. For example if my JTable is:
and there is just String, the correspondig histogram is nice:
--edit1
What dou you think about StackedBarChart3D? I get this output:
My histogram has two targets:
You may get a more appealing result with ChartFactory.createHistogram() and a SimpleHistogramDataset, seen here.
To get diverse colors, override the getItemPaint() method in a custom XYBarRenderer, as suggested here.
I have a data source in which there are three departments and each department has equal employees that are 8.
I want to make a pie chart using jFreeChart such that first we partition the pie into 3 equal parts for departments that is 120' for each department. Then in these partitions I want to show the sales of each employee. How can I do this in jFreeChart.
public class PieChart extends JFrame {
private PieDataset createDataset() {
DefaultPieDataset result = new DefaultPieDataset();
result.setValue("department1", 33.33);
result.setValue("department2", 33.33);
result.setValue("department3", 33.33);
return result;
}
private JFreeChart createChart(PieDataset dataset, String title) {
JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title
dataset, // data
true, // include legend
true,
false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart;
}
}
public static void main(String[] args) {
PieChart demo = new PieChart("Comparison", "Which operating system are you using?");
demo.pack();
demo.setVisible(true);
}
PieChartDemo1 is a good starting point; focus on createDataset(); the full source is included in the distribution.
Addendum: How to further create partitions?
Ah, you want to sub-divide each 120° partition. DefaultPieDataset doesn't support a hierarchical structure directly, but you can use color in the PiePlot to highlight the grouping. Create related colors using Color.getHSBColor(), as shown here, and use setSectionPaint() to apply the colors accordingly.
i'm using jfreechart, and i need to add labels to my series data. There are bold dots on graphic and they need labels... Following code does not work.
XYSeries series = new XYSeries("Average Size");
series.add(.60, .70);
XYDataset xyDataset = new XYSeriesCollection(series);
XYItemRenderer rend = new XYShapeRenderer();
XYItemLabelGenerator generator = new XYItemLabelGenerator() {
#Override
public String generateLabel(XYDataset xyd, int i, int i1) {
return "Some label?";
}
};
//SeriesItemLabelGenerator
rend.setBaseItemLabelGenerator(generator);
rend.setBaseItemLabelsVisible(true);
ItemLabelPosition pos = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT);
rend.setBasePositiveItemLabelPosition(pos);
I think the problem is that XYShapeRenderer(http://www.jfree.org/jfreechart/api/javadoc/src-html/org/jfree/chart/renderer/xy/XYShapeRenderer.html), which extends AbstractXYItemRenderer, does not implement ItemLabelGenerator logic yet.
So, or you will need to use another Renderer.
For example, XYLineAndShapeRenderer(http://www.jfree.org/jfreechart/api/javadoc/src-html/org/jfree/chart/renderer/xy/XYLineAndShapeRenderer.html) implements it:
// draw the item label if there is one...
if (isItemLabelVisible(series, item)) {
drawItemLabel(g2, orientation, dataset, series, item, xx, yy,(y1 < 0.0));
}
Or you will need to extend XYShapeRenderer yourself and add the label drawing logic, using any of the other Renderer's source code as an example.
I am using JFreeChart to create candlestick charts in my Java app. However, my charts end up looking like this:
http://imageshack.us/photo/my-images/69/capturebhx.png/
I would like to have the Y-axis automatically scaled so that the chart looks more like this:
http://imageshack.us/photo/my-images/717/capture2wl.png/
I think org.jfree.chart.axis.NumberAxis.configure() will do this, but I'm not sure. I can't seem to find a way to tie my JFreeChartobject, or ChartPanel object to this NumberAxis object. Please help me, I am lost and have been looking for a long time to try and tie these objects together. Or, if you can find another way, that'd be great too!
Some code:
...
private DefaultHighLowDataset dataset;
private JFreeChart chart;
private ChartPanel chart_panel;
...
// creates dataset, then chart from dataset, then chart_panel from chart
dataset = new DefaultHighLowDataset("", date, high, low, open, close, volume);
chart = ChartFactory.createCandlestickChart("Blank", "Time", "Price", dataset, false);
chart_panel = new ChartPanel(chart); // what you see in the images
...
Be sure to setAutoRangeIncludesZero(false) or "the axis range…is forced to include zero."
Addendum:
I still don't know how to link a NumberAxis object to a ChartPanel object or JFreeChart object.
You may want to look into the examples in org.jfree.chart.demo and here. If this is terra incognita, I'd recommend The JFreeChart Developer Guide†.
†Disclaimer: Not affiliated with Object Refinery Limited; just a satisfied customer and very minor contributor.
I did it like this:
final JFreeChart chart = ChartFactory.createCandlestickChart(
"Candlestick Demo", "Time", "Price", dataset, false);
double lowestLow = getLowestLow(dataset);
double highestHigh = getHighestHigh(dataset);
chart.getXYPlot().getRangeAxis().setRange(lowestLow*0.95, highestHigh*1.05);
I calculate the lowest low and lowest high using these functions
private double getLowestLow(DefaultHighLowDataset dataset){
double lowest;
lowest = dataset.getLowValue(0,0);
for(int i=1;i<dataset.getItemCount(0);i++){
if(dataset.getLowValue(0,i) < lowest){
lowest = dataset.getLowValue(0,i);
}
}
return lowest;
}
private double getHighestHigh(DefaultHighLowDataset dataset){
double highest;
highest = dataset.getHighValue(0,0);
for(int i=1;i<dataset.getItemCount(0);i++){
if(dataset.getLowValue(0,i) > highest){
highest = dataset.getHighValue(0,i);
}
}
return highest;
}
This seems to give me a very nice candlestick chart that makes good use of the Y-axis range. Hope this helps.
I have a JFreeChart instance that displays process memory status, initialized as follows:
m_data = new TimeSeriesCollection();
TimeSeries vmsize = new TimeSeries("VMSize");
TimeSeries resident = new TimeSeries("Resisdent");
TimeSeries shared = new TimeSeries("Shared memory");
TimeSeries code = new TimeSeries("Code");
TimeSeries data = new TimeSeries("Data");
m_data.addSeries(vmsize);
m_data.addSeries(resident);
m_data.addSeries(shared);
m_data.addSeries(code);
m_data.addSeries(data);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Memory usage", "Time", "Size", m_data, true, true, false);
m_chart = new ChartPanel(chart);
Later I add values to each TimeSeries in the TimeSeriesCollection. I would like to somehow know - when the user clicks on the Chart - either what time associated with that columm, or even better - what is the index of the value.
I looked at the JFreeChart and ChartMouseListener classes, but I could not figure out how to do that (also the documentation of JFreeChart is annoyingly scarce, I guess they are trying to get people to buy their developer's guide).
if you click dead on the item, the event.getEntity() function returns XYItem and then from there onwards
XYItemEntity xyitem=(XYItemEntity) event.getEntity(); // get clicked entity
XYDataset dataset = (XYDataset)xyitem.getDataset(); // get data set
System.out.println(xyitem.getItem()+" item of "+xyitem.getSeriesIndex()+"series");
System.out.println(dataset.getXValue(xyitem.getSeriesIndex(), xyitem.getItem()));
System.out.println(dataset.getYValue(xyitem.getSeriesIndex(), xyitem.getItem()));
Comparable comparable=dataset.getSeriesKey(0);
XYPlot xyplot = (XYPlot) event.getChart().getPlot();
System.out.println(xyplot.getRangeCrosshairValue());
however incase you do not click on the item itself but your crosshair is set to auto lock on data, in such case the crosshair will move to nearest item but since the item has not been clicked, you will not be able to get the XYItem and hence you cannot know the series and item index, to solve this problem there is this code below, it should be put in the catch clause while the above mentioned code should be in try clause
first define a function which will take crosshair value at domain and range and also Xydataset, this functions returns an inner class object that groups item index and series index
public static SeriesAndItemIndex getItemIndex(double domainVal,double rangeVal,XYDataset xydataset){
Comparable comparable;
int indexOf;
for(int i=0;i<xydataset.getSeriesCount();i++){
comparable = xydataset.getSeriesKey(i);
indexOf=xydataset.indexOf(comparable);
for(int j=0 ; j<xydataset.getItemCount(indexOf);j++){
double x=xydataset.getXValue(indexOf, j);
double y=xydataset.getYValue(indexOf, j);
if(x == domainVal && y==rangeVal){
return new SeriesAndItemIndex(j,indexOf);//return item index and series index
}
}
}
return null;
}
private static class SeriesAndItemIndex{ ///inner CLASS to group series and item clicked index
public int itemIndex;
public int seriesIndex;
public SeriesAndItemIndex(int i,int s){
itemIndex=i;
seriesIndex=s;
}
#Override
public String toString(){
return "itemIndex="+itemIndex+",seriesIndex="+seriesIndex;
}
}
how to use it?
try{......code block from the top
}catch(Exception e){
Object source=event.getSource();
JFreeChart chartpanel=(JFreeChart)source;
XYPlot xyplot = (XYPlot) chartpanel.getPlot();
XYDataset xydataset= xyplot.getDataset();
double d=xyplot.getDomainCrosshairValue(); //get crosshair X value
double r =xyplot.getRangeCrosshairValue(); //get crosshair y value
SeriesAndItemIndex index=getItemIndex(d,r,xydataset);
if(index != null){
System.out.println(index.toString());
}
}
hmm should work, if you replace the last two lines by something like this:
ChartPanel panel=new ChartPanel(ChartFactory.createTimeSeriesChart("Memory usage", "Time", "Size", m_data, true, true, false)));
panel.addChartMouseListener(new ChartMouseListener(){
void chartMouseClicked(ChartMouseEvent e){
[...do something on click...]
}
void chartMouseMoved(ChartMouseEvent e){
[...do something on move...]
}
});
return panel;