在现有饼图下方添加折线图
Add a line chart beneath an existing pie chart
当我点击饼图的一部分时,我想在第一个图表下看到另一个图表(折线图)。现在我生成了另一个面板图表,但我丢失了第一个面板,因为第二个图表绘制在第一个面板上,第二个绘制第一个图表,但第二个图表的尺寸不好;看图。
我该如何调整我的问题?
JFreeChart chart = ChartFactory.createPieChart("Pratiche complessive",
dataset, true, true, false);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(560, 370));
PiePlot plot = (PiePlot) chart.getPlot();
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
"{1} pratica/che");
plot.setLabelFont(new Font("Courier New", Font.BOLD, 10));
plot.setLabelLinkPaint(Color.BLACK);
plot.setLabelLinkStroke(new BasicStroke(1.0f));
plot.setLabelOutlineStroke(null);
plot.setLabelPaint(Color.BLUE);
plot.setLabelBackgroundPaint(null);
plot.setLabelGenerator(gen);
chart.setBackgroundPaint(Color.orange);
chartPanel.addChartMouseListener(this);
this.setContentPane(chartPanel);
}
public void chartMouseClicked(ChartMouseEvent event) {
ChartEntity entity = event.getEntity();
String sezione = "";
sezione = entity.toString().substring(17);
sezione = sezione.replace(")", "");
System.out.println(sezione);
// PieSection: 0, 0(ARCHIVIATO)===>ARCHIVIATO V
if (entity != null) {
try {
String query = query;
String numero_pratiche = "";
String nome_stato = "";
String data_modifica;
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
while (rs.next()) {
dataset
}
// System.out.println(entity.toString());
JFreeChart lineChart = ChartFactory.createLineChart("Pratiche", "Data", "Pratiche", dataset, PlotOrientation.VERTICAL,true, true, false);
ChartPanel pannello_dettaglio = new ChartPanel(lineChart);
pannello_dettaglio.setPreferredSize(new java.awt.Dimension(560,367));
this.setContentPane(pannello_dettaglio);
JfreeChart dettaglio = new JfreeChart("Dettaglio");
pannello_dettaglio.setSize(560, 367);
RefineryUtilities.centerFrameOnScreen(dettaglio);
dettaglio.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//first panel(piechart)
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
JfreeChart demo = new JfreeChart("Pratiche complessive");
demo.setSize(560, 367);
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
这是我的第一个图表饼图:
这是我的最终结果:
I would like to see one panel chart top-to-bottom.
您似乎正在用折线图面板替换整个饼图面板。相反,添加两个图表面板并更新 ChartMouseListener
中折线图的数据集。收听折线图将自行更新以作出响应。在下面的示例中,侦听器更新折线图的数据集以反映单击了哪个饼图部分。作为参考,它还显示相应的 ChartEntity
.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.PieSectionEntity;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
/**
* @see
*/
public class PieLineTest {
public void display() {
final DefaultPieDataset pieData = new DefaultPieDataset();
pieData.setValue("One", 42);
pieData.setValue("Two", 84);
JFreeChart pieChart = ChartFactory.createPieChart(
"Title", pieData, true, true, false);
DefaultCategoryDataset lineData = new DefaultCategoryDataset();
JFreeChart lineChart = ChartFactory.createLineChart(
"Title", "Domain", "Range", lineData);
ChartPanel piePanel = new ChartPanel(pieChart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
};
piePanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent cme) {
updateLineData(cme);
}
@Override
public void chartMouseMoved(ChartMouseEvent cme) {
updateLineData(cme);
}
private void updateLineData(ChartMouseEvent cme) {
ChartEntity ce = cme.getEntity();
lineChart.setTitle(ce.getClass().getSimpleName());
lineData.clear();
if (ce instanceof PieSectionEntity) {
PieSectionEntity e = (PieSectionEntity) ce;
DefaultPieDataset d = (DefaultPieDataset) e.getDataset();
Comparable sectionKey = e.getSectionKey();
lineData.addValue(1, e.toString(), "Begin");
lineData.addValue(d.getValue(sectionKey), e.toString(), "End");
}
}
});
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(0, 1));
f.add(piePanel);
f.add(new ChartPanel(lineChart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new PieLineTest()::display);
}
}
当我点击饼图的一部分时,我想在第一个图表下看到另一个图表(折线图)。现在我生成了另一个面板图表,但我丢失了第一个面板,因为第二个图表绘制在第一个面板上,第二个绘制第一个图表,但第二个图表的尺寸不好;看图。
我该如何调整我的问题?
JFreeChart chart = ChartFactory.createPieChart("Pratiche complessive",
dataset, true, true, false);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(560, 370));
PiePlot plot = (PiePlot) chart.getPlot();
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
"{1} pratica/che");
plot.setLabelFont(new Font("Courier New", Font.BOLD, 10));
plot.setLabelLinkPaint(Color.BLACK);
plot.setLabelLinkStroke(new BasicStroke(1.0f));
plot.setLabelOutlineStroke(null);
plot.setLabelPaint(Color.BLUE);
plot.setLabelBackgroundPaint(null);
plot.setLabelGenerator(gen);
chart.setBackgroundPaint(Color.orange);
chartPanel.addChartMouseListener(this);
this.setContentPane(chartPanel);
}
public void chartMouseClicked(ChartMouseEvent event) {
ChartEntity entity = event.getEntity();
String sezione = "";
sezione = entity.toString().substring(17);
sezione = sezione.replace(")", "");
System.out.println(sezione);
// PieSection: 0, 0(ARCHIVIATO)===>ARCHIVIATO V
if (entity != null) {
try {
String query = query;
String numero_pratiche = "";
String nome_stato = "";
String data_modifica;
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
while (rs.next()) {
dataset
}
// System.out.println(entity.toString());
JFreeChart lineChart = ChartFactory.createLineChart("Pratiche", "Data", "Pratiche", dataset, PlotOrientation.VERTICAL,true, true, false);
ChartPanel pannello_dettaglio = new ChartPanel(lineChart);
pannello_dettaglio.setPreferredSize(new java.awt.Dimension(560,367));
this.setContentPane(pannello_dettaglio);
JfreeChart dettaglio = new JfreeChart("Dettaglio");
pannello_dettaglio.setSize(560, 367);
RefineryUtilities.centerFrameOnScreen(dettaglio);
dettaglio.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//first panel(piechart)
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
JfreeChart demo = new JfreeChart("Pratiche complessive");
demo.setSize(560, 367);
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
这是我的第一个图表饼图:
这是我的最终结果:
I would like to see one panel chart top-to-bottom.
您似乎正在用折线图面板替换整个饼图面板。相反,添加两个图表面板并更新 ChartMouseListener
中折线图的数据集。收听折线图将自行更新以作出响应。在下面的示例中,侦听器更新折线图的数据集以反映单击了哪个饼图部分。作为参考,它还显示相应的 ChartEntity
.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.PieSectionEntity;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
/**
* @see
*/
public class PieLineTest {
public void display() {
final DefaultPieDataset pieData = new DefaultPieDataset();
pieData.setValue("One", 42);
pieData.setValue("Two", 84);
JFreeChart pieChart = ChartFactory.createPieChart(
"Title", pieData, true, true, false);
DefaultCategoryDataset lineData = new DefaultCategoryDataset();
JFreeChart lineChart = ChartFactory.createLineChart(
"Title", "Domain", "Range", lineData);
ChartPanel piePanel = new ChartPanel(pieChart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
};
piePanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent cme) {
updateLineData(cme);
}
@Override
public void chartMouseMoved(ChartMouseEvent cme) {
updateLineData(cme);
}
private void updateLineData(ChartMouseEvent cme) {
ChartEntity ce = cme.getEntity();
lineChart.setTitle(ce.getClass().getSimpleName());
lineData.clear();
if (ce instanceof PieSectionEntity) {
PieSectionEntity e = (PieSectionEntity) ce;
DefaultPieDataset d = (DefaultPieDataset) e.getDataset();
Comparable sectionKey = e.getSectionKey();
lineData.addValue(1, e.toString(), "Begin");
lineData.addValue(d.getValue(sectionKey), e.toString(), "End");
}
}
});
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(0, 1));
f.add(piePanel);
f.add(new ChartPanel(lineChart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new PieLineTest()::display);
}
}