I want edit a value of line chart by clicking and dragging, this is possible ?
Thanks.
Edit value of chart
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: Edit value of chart
In general, yes.
What type of Plot do you use?
What type of Plot do you use?
Re: Edit value of chart
This is my code, to create a line chart:
And here I get the mouse click:
Code: Select all
public static JFreeChart createChart(final XYDataset dataset, String title) {
// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart(
title, // chart title
"", // x axis label
"", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
false, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
chart.setBackgroundPaint(Color.white);
//final StandardLegend legend = (StandardLegend) chart.getLegend();
//legend.setDisplaySeriesShapes(true);
// get a reference to the plot for further customisation...
final XYPlot plot = chart.getXYPlot();
//plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.BLACK);
plot.setBackgroundPaint(Color.WHITE);
//final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
//renderer.setSeriesLinesVisible(0, false);
//renderer.setSeriesShapesVisible(1, false);
//plot.setRenderer(renderer);
//Oculta a borda.
plot.setOutlineVisible(false);
chart.setBorderVisible(false);
//Oculta os eixos.
plot.getDomainAxis().setVisible(false); //Oculta o eixo X.
chart.removeLegend();
chart.clearSubtitles();
return chart;
}
And here I get the mouse click:
Code: Select all
chartPanel.addChartMouseListener(new Edit_Chart.MyChartMouseListener());
Code: Select all
private static class MyChartMouseListener implements ChartMouseListener {
public MyChartMouseListener() {
}
@Override
public void chartMouseClicked(ChartMouseEvent event) {
ChartEntity entity = event.getEntity();
if (entity == null) {
return;
}
if (entity instanceof XYItemEntity) {
// Get entity details
String tooltip = ((XYItemEntity) entity).getToolTipText();
XYDataset dataset = ((XYItemEntity) entity).getDataset();
int seriesIndex = ((XYItemEntity) entity).getSeriesIndex();
int item = ((XYItemEntity) entity).getItem();
// You have the dataset the data point belongs to, the index of the series in that dataset of the data point, and the specific item index in the series of the data point.
XYSeries series = ((XYSeriesCollection) dataset).getSeries(seriesIndex);
XYDataItem xyItem = series.getDataItem(item);
xyItem.setY(xyItem.getYValue()-1);
}
}
@Override
public void chartMouseMoved(ChartMouseEvent event) {
}
}
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: Edit value of chart
Is this a solution for your problem or a better description?
Re: Edit value of chart
It is a better description. In this code I get a value when I click, and subtract -1. But what I need is click and drag changing the value.
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: Edit value of chart
I am afraid that you will have to implement your logic in a custom MouseListener that you add to the ChartPanel.
Everything that is happening inside the mouseClicked(MouseEvent me) and mouseDragged(MouseEvent me) methods of ChartPanel itself should also be doable in an "external" MouseListener, namely the following actions:
- get coordinates of the MouseEvent
- convert the mouse coordinates to data coordinates of the JFreeChart by taking the insets of teh ChartPanel and its scale factors into account
- check whether the ChartRenderingInfo of the ChartPanel has an entity at the data coordinates
- finally, determine the entity type and use the rest of the logic that you have already implementedl.
Everything that is happening inside the mouseClicked(MouseEvent me) and mouseDragged(MouseEvent me) methods of ChartPanel itself should also be doable in an "external" MouseListener, namely the following actions:
- get coordinates of the MouseEvent
- convert the mouse coordinates to data coordinates of the JFreeChart by taking the insets of teh ChartPanel and its scale factors into account
- check whether the ChartRenderingInfo of the ChartPanel has an entity at the data coordinates
- finally, determine the entity type and use the rest of the logic that you have already implementedl.