Autopsy  4.19.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 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 dataset = new DefaultPieDataset();
63  private final JFreeChart chart;
64  private final PiePlot plot;
65 
69  public PieChartPanel() {
70  this(null);
71  }
72 
78  public PieChartPanel(String title) {
79  // Create chart
80  this.chart = ChartFactory.createPieChart(
81  title,
82  dataset,
83  false,
84  false,
85  false);
86 
87  chart.setBackgroundPaint(null);
88  chart.getTitle().setFont(DEFAULT_HEADER_FONT);
89 
90  this.plot = ((PiePlot) chart.getPlot());
91  plot.setInteriorGap(DEFAULT_CHART_PADDING);
92  plot.setLabelGenerator(DEFAULT_LABEL_GENERATOR);
93  plot.setLabelFont(DEFAULT_FONT);
94  plot.setBackgroundPaint(null);
95  plot.setOutlinePaint(null);
96 
97  // Create Panel
98  ChartPanel panel = new ChartPanel(chart);
99  panel.addOverlay(overlay);
100  panel.setPopupMenu(null);
101 
102  this.setLayout(new BorderLayout());
103  this.add(panel, BorderLayout.CENTER);
104  }
105 
109  public String getTitle() {
110  return (this.chart == null || this.chart.getTitle() == null)
111  ? null
112  : this.chart.getTitle().getText();
113  }
114 
122  public PieChartPanel setTitle(String title) {
123  this.chart.getTitle().setText(title);
124  return this;
125  }
126 
127  @Override
128  protected void setMessage(boolean visible, String message) {
129  this.overlay.setVisible(visible);
130  this.overlay.setMessage(message);
131  }
132 
133  @Override
134  protected void setResults(List<PieChartItem> data) {
135  this.dataset.clear();
136  this.plot.clearSectionPaints(false);
137 
138  if (data != null && !data.isEmpty()) {
139  for (PieChartItem slice : data) {
140  this.dataset.setValue(slice.getLabel(), slice.getValue());
141  if (slice.getColor() != null) {
142  this.plot.setSectionPaint(slice.getLabel(), slice.getColor());
143  }
144  }
145  } else {
146  // show a no data label if no data.
147  // this in fact shows a very small number for the value
148  // that should be way below rounding error for formatters
149  this.dataset.setValue(Bundle.PieChartPanel_noDataLabel(), NEAR_ZERO);
150  this.plot.setSectionPaint(Bundle.PieChartPanel_noDataLabel(), NO_DATA_COLOR);
151  }
152  }
153 
160  public synchronized void showDataWithMessage(List<PieChartItem> data, String message) {
161  setResults(data);
162  setMessage(true, message);
163  repaint();
164  }
165 }
synchronized void showDataWithMessage(List< PieChartItem > data, String message)

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