Autopsy  4.17.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.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<PieChartPanel.PieChartItem>> {
43 
47  public static class PieChartItem {
48 
49  private final String label;
50  private final double value;
51  private final Color color;
52 
61  public PieChartItem(String label, double value, Color color) {
62  this.label = label;
63  this.value = value;
64  this.color = color;
65  }
66 
70  public String getLabel() {
71  return label;
72  }
73 
77  public double getValue() {
78  return value;
79  }
80 
84  public Color getColor() {
85  return color;
86  }
87  }
88 
89  private static final long serialVersionUID = 1L;
90 
91  private static final Font DEFAULT_FONT = new JLabel().getFont();
92 
97  private static final double NEAR_ZERO = Math.ulp(1d);
98  private static final Color NO_DATA_COLOR = Color.WHITE;
99  private static final double DEFAULT_CHART_PADDING = .1;
100 
101  private static final Font DEFAULT_HEADER_FONT = new Font(DEFAULT_FONT.getName(), DEFAULT_FONT.getStyle(), (int) (DEFAULT_FONT.getSize() * 1.5));
102  private static final PieSectionLabelGenerator DEFAULT_LABEL_GENERATOR
103  = new StandardPieSectionLabelGenerator(
104  "{0}: {1} ({2})", new DecimalFormat("#,###"), new DecimalFormat("0.0%"));
105 
106  private final ChartMessageOverlay overlay = new ChartMessageOverlay();
107  private final DefaultPieDataset dataset = new DefaultPieDataset();
108  private final JFreeChart chart;
109  private final PiePlot plot;
110 
114  public PieChartPanel() {
115  this(null);
116  }
117 
123  public PieChartPanel(String title) {
124  // Create chart
125  this.chart = ChartFactory.createPieChart(
126  title,
127  dataset,
128  false,
129  false,
130  false);
131 
132  chart.setBackgroundPaint(null);
133  chart.getTitle().setFont(DEFAULT_HEADER_FONT);
134 
135  this.plot = ((PiePlot) chart.getPlot());
136  plot.setInteriorGap(DEFAULT_CHART_PADDING);
137  plot.setLabelGenerator(DEFAULT_LABEL_GENERATOR);
138  plot.setLabelFont(DEFAULT_FONT);
139  plot.setBackgroundPaint(null);
140  plot.setOutlinePaint(null);
141 
142  // Create Panel
143  ChartPanel panel = new ChartPanel(chart);
144  panel.addOverlay(overlay);
145  panel.setPopupMenu(null);
146 
147  this.setLayout(new BorderLayout());
148  this.add(panel, BorderLayout.CENTER);
149  }
150 
154  public String getTitle() {
155  return (this.chart == null || this.chart.getTitle() == null)
156  ? null
157  : this.chart.getTitle().getText();
158  }
159 
167  public PieChartPanel setTitle(String title) {
168  this.chart.getTitle().setText(title);
169  return this;
170  }
171 
172  @Override
173  protected void setMessage(boolean visible, String message) {
174  this.overlay.setVisible(visible);
175  this.overlay.setMessage(message);
176  }
177 
178  @Override
179  protected void setResults(List<PieChartPanel.PieChartItem> data) {
180  this.dataset.clear();
181  this.plot.clearSectionPaints(false);
182 
183  if (data != null && !data.isEmpty()) {
184  for (PieChartPanel.PieChartItem slice : data) {
185  this.dataset.setValue(slice.getLabel(), slice.getValue());
186  if (slice.getColor() != null) {
187  this.plot.setSectionPaint(slice.getLabel(), slice.getColor());
188  }
189  }
190  } else {
191  // show a no data label if no data.
192  // this in fact shows a very small number for the value
193  // that should be way below rounding error for formatters
194  this.dataset.setValue(Bundle.PieChartPanel_noDataLabel(), NEAR_ZERO);
195  this.plot.setSectionPaint(Bundle.PieChartPanel_noDataLabel(), NO_DATA_COLOR);
196  }
197  }
198 
205  public synchronized void showDataWithMessage(List<PieChartPanel.PieChartItem> data, String message) {
206  setResults(data);
207  setMessage(true, message);
208  repaint();
209  }
210 }
synchronized void showDataWithMessage(List< PieChartPanel.PieChartItem > data, String message)
void setResults(List< PieChartPanel.PieChartItem > data)

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