Autopsy  4.16.0
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 2019 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.Font;
23 import java.awt.Graphics2D;
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.panel.AbstractOverlay;
33 import org.jfree.chart.panel.Overlay;
34 import org.jfree.chart.plot.PiePlot;
35 import org.jfree.data.general.DefaultPieDataset;
36 import org.openide.util.NbBundle.Messages;
37 
41 @Messages({
42  "PieChartPanel_noDataLabel=No Data"
43 })
44 public class PieChartPanel extends AbstractLoadableComponent<List<PieChartPanel.PieChartItem>> {
45 
49  public static class PieChartItem {
50 
51  private final String label;
52  private final double value;
53 
60  public PieChartItem(String label, double value) {
61  this.label = label;
62  this.value = value;
63  }
64 
68  public String getLabel() {
69  return label;
70  }
71 
75  public double getValue() {
76  return value;
77  }
78  }
79 
84  private static class MessageOverlay extends AbstractOverlay implements Overlay {
85 
86  private static final long serialVersionUID = 1L;
87  private final BaseMessageOverlay overlay = new BaseMessageOverlay();
88 
95  void setVisible(boolean visible) {
96  overlay.setVisible(visible);
97  }
98 
104  void setMessage(String message) {
105  overlay.setMessage(message);
106  }
107 
108  @Override
109  public void paintOverlay(Graphics2D gd, ChartPanel cp) {
110  overlay.paintOverlay(gd, cp.getWidth(), cp.getHeight());
111  }
112 
113  }
114 
115  private static final long serialVersionUID = 1L;
116 
117  private static final Font DEFAULT_FONT = new JLabel().getFont();
118 
123  private static final double NEAR_ZERO = Math.ulp(1d);
124 
125  private static final Font DEFAULT_HEADER_FONT = new Font(DEFAULT_FONT.getName(), DEFAULT_FONT.getStyle(), (int) (DEFAULT_FONT.getSize() * 1.5));
126  private static final PieSectionLabelGenerator DEFAULT_LABEL_GENERATOR
127  = new StandardPieSectionLabelGenerator(
128  "{0}: {1} ({2})", new DecimalFormat("#,###"), new DecimalFormat("0.0%"));
129 
130  private final MessageOverlay overlay = new MessageOverlay();
131  private final DefaultPieDataset dataset = new DefaultPieDataset();
132  private final JFreeChart chart;
133 
137  public PieChartPanel() {
138  this(null);
139  }
140 
146  public PieChartPanel(String title) {
147  // Create chart
148  this.chart = ChartFactory.createPieChart(
149  title,
150  dataset,
151  true,
152  true,
153  false);
154 
155  chart.setBackgroundPaint(null);
156  chart.getLegend().setItemFont(DEFAULT_FONT);
157  chart.getTitle().setFont(DEFAULT_HEADER_FONT);
158 
159  // don't show a legend by default
160  chart.removeLegend();
161 
162  PiePlot plot = ((PiePlot) chart.getPlot());
163 
164  plot.setLabelGenerator(DEFAULT_LABEL_GENERATOR);
165  plot.setLabelFont(DEFAULT_FONT);
166  plot.setBackgroundPaint(null);
167  plot.setOutlinePaint(null);
168 
169  // Create Panel
170  ChartPanel panel = new ChartPanel(chart);
171  panel.addOverlay(overlay);
172  this.setLayout(new BorderLayout());
173  this.add(panel, BorderLayout.CENTER);
174  }
175 
179  public String getTitle() {
180  return (this.chart == null || this.chart.getTitle() == null)
181  ? null
182  : this.chart.getTitle().getText();
183  }
184 
192  public PieChartPanel setTitle(String title) {
193  this.chart.getTitle().setText(title);
194  return this;
195  }
196 
197  @Override
198  protected void setMessage(boolean visible, String message) {
199  this.overlay.setVisible(visible);
200  this.overlay.setMessage(message);
201  }
202 
203  @Override
204  protected void setResults(List<PieChartPanel.PieChartItem> data) {
205  this.dataset.clear();
206  if (data != null && !data.isEmpty()) {
207  for (PieChartPanel.PieChartItem slice : data) {
208  this.dataset.setValue(slice.getLabel(), slice.getValue());
209  }
210  } else {
211  // show a no data label if no data.
212  // this in fact shows a very small number for the value
213  // that should be way below rounding error for formatters
214  this.dataset.setValue(Bundle.PieChartPanel_noDataLabel(), NEAR_ZERO);
215  }
216  }
217 
224  public synchronized void showDataWithMessage(List<PieChartPanel.PieChartItem> data, String message) {
225  setResults(data);
226  setMessage(true, message);
227  repaint();
228  }
229 }
synchronized void showDataWithMessage(List< PieChartPanel.PieChartItem > data, String message)
void setResults(List< PieChartPanel.PieChartItem > data)

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.