Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
PieChartPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.datasourcesummary.uiutils;
20 
21 import java.awt.BorderLayout;
22 import java.awt.Color;
23 import java.awt.Font;
24 import java.text.DecimalFormat;
25 import java.util.List;
26 import javax.swing.JLabel;
27 import org.jfree.chart.ChartFactory;
28 import org.jfree.chart.ChartPanel;
29 import org.jfree.chart.JFreeChart;
30 import org.jfree.chart.labels.PieSectionLabelGenerator;
31 import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
32 import org.jfree.chart.plot.PiePlot;
33 import org.jfree.data.general.DefaultPieDataset;
34 import org.openide.util.NbBundle.Messages;
35 
39 @Messages({
40  "PieChartPanel_noDataLabel=No Data"
41 })
42 public class PieChartPanel extends AbstractLoadableComponent<List<PieChartItem>> {
43 
44  private static final long serialVersionUID = 1L;
45 
46  private static final Font DEFAULT_FONT = new JLabel().getFont();
47 
52  private static final double NEAR_ZERO = Math.ulp(1d);
53  private static final Color NO_DATA_COLOR = Color.WHITE;
54  private static final double DEFAULT_CHART_PADDING = .1;
55 
56  private static final Font DEFAULT_HEADER_FONT = new Font(DEFAULT_FONT.getName(), DEFAULT_FONT.getStyle(), (int) (DEFAULT_FONT.getSize() * 1.5));
57  private static final PieSectionLabelGenerator DEFAULT_LABEL_GENERATOR
58  = new StandardPieSectionLabelGenerator(
59  "{0}: {1} ({2})", new DecimalFormat("#,###"), new DecimalFormat("0.0%"));
60 
61  private final ChartMessageOverlay overlay = new ChartMessageOverlay();
62  private final DefaultPieDataset<String> dataset = new DefaultPieDataset<>();
63  private final JFreeChart chart;
64  private final PiePlot<String> plot;
65 
66  @SuppressWarnings("unchecked")
67  private static PiePlot<String> getTypedPlot(JFreeChart chart) {
68  return ((PiePlot<String>) chart.getPlot());
69  }
70 
71 
75  public PieChartPanel() {
76  this(null);
77  }
78 
84  public PieChartPanel(String title) {
85  // Create chart
86  this.chart = ChartFactory.createPieChart(
87  title,
88  dataset,
89  false,
90  false,
91  false);
92 
93  chart.setBackgroundPaint(null);
94  chart.getTitle().setFont(DEFAULT_HEADER_FONT);
95  this.plot = getTypedPlot(chart);
96  plot.setInteriorGap(DEFAULT_CHART_PADDING);
97  plot.setLabelGenerator(DEFAULT_LABEL_GENERATOR);
98  plot.setLabelFont(DEFAULT_FONT);
99  plot.setBackgroundPaint(null);
100  plot.setOutlinePaint(null);
101 
102  // Create Panel
103  ChartPanel panel = new ChartPanel(chart);
104  panel.addOverlay(overlay);
105  panel.setPopupMenu(null);
106 
107  this.setLayout(new BorderLayout());
108  this.add(panel, BorderLayout.CENTER);
109  }
110 
114  public String getTitle() {
115  return (this.chart == null || this.chart.getTitle() == null)
116  ? null
117  : this.chart.getTitle().getText();
118  }
119 
127  public PieChartPanel setTitle(String title) {
128  this.chart.getTitle().setText(title);
129  return this;
130  }
131 
132  @Override
133  protected void setMessage(boolean visible, String message) {
134  this.overlay.setVisible(visible);
135  this.overlay.setMessage(message);
136  }
137 
138  @Override
139  protected void setResults(List<PieChartItem> data) {
140  this.dataset.clear();
141  this.plot.clearSectionPaints(false);
142 
143  if (data != null && !data.isEmpty()) {
144  for (PieChartItem slice : data) {
145  this.dataset.setValue(slice.getLabel(), slice.getValue());
146  if (slice.getColor() != null) {
147  this.plot.setSectionPaint(slice.getLabel(), slice.getColor());
148  }
149  }
150  } else {
151  // show a no data label if no data.
152  // this in fact shows a very small number for the value
153  // that should be way below rounding error for formatters
154  this.dataset.setValue(Bundle.PieChartPanel_noDataLabel(), NEAR_ZERO);
155  this.plot.setSectionPaint(Bundle.PieChartPanel_noDataLabel(), NO_DATA_COLOR);
156  }
157  }
158 
165  public synchronized void showDataWithMessage(List<PieChartItem> data, String message) {
166  setResults(data);
167  setMessage(true, message);
168  repaint();
169  }
170 }
synchronized void showDataWithMessage(List< PieChartItem > data, String message)

Copyright © 2012-2022 Basis Technology. Generated on: Sat Nov 26 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.