Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
BarChartPanel.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.util.Collections;
25 import java.util.List;
26 import javax.swing.JLabel;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.jfree.chart.ChartFactory;
29 import org.jfree.chart.ChartPanel;
30 import org.jfree.chart.JFreeChart;
31 import org.jfree.chart.axis.ValueAxis;
32 import org.jfree.chart.plot.CategoryPlot;
33 import org.jfree.chart.plot.PlotOrientation;
34 import org.jfree.chart.renderer.category.BarRenderer;
35 import org.jfree.chart.renderer.category.StandardBarPainter;
36 import org.jfree.data.category.DefaultCategoryDataset;
37 
41 public class BarChartPanel extends AbstractLoadableComponent<List<BarChartPanel.BarChartSeries>> {
42 
47  public static class BarChartSeries {
48 
49  private final Comparable<?> key;
50  private final Color color;
51  private final List<BarChartItem> items;
52 
60  public BarChartSeries(Comparable<?> key, Color color, List<BarChartItem> items) {
61  this.key = key;
62  this.color = color;
63  this.items = (items == null) ? Collections.emptyList() : Collections.unmodifiableList(items);
64  }
65 
69  public Color getColor() {
70  return color;
71  }
72 
76  public List<BarChartItem> getItems() {
77  return items;
78  }
79 
83  public Comparable<?> getKey() {
84  return key;
85  }
86  }
87 
91  public static class BarChartItem {
92 
93  private final Comparable<?> key;
94  private final double value;
95 
102  public BarChartItem(Comparable<?> key, double value) {
103  this.key = key;
104  this.value = value;
105  }
106 
110  public Comparable<?> getKey() {
111  return key;
112  }
113 
117  public double getValue() {
118  return value;
119  }
120  }
121 
127  public static class OrderedKey implements Comparable<OrderedKey> {
128 
129  private final Object keyValue;
130  private final int keyIndex;
131 
139  public OrderedKey(Object keyValue, int keyIndex) {
140  this.keyValue = keyValue;
141  this.keyIndex = keyIndex;
142  }
143 
147  Object getKeyValue() {
148  return keyValue;
149  }
150 
154  int getKeyIndex() {
155  return keyIndex;
156  }
157 
158  @Override
159  public int compareTo(OrderedKey o) {
160  // this will have a higher value than null.
161  if (o == null) {
162  return 1;
163  }
164 
165  // compare by index
166  return Integer.compare(this.getKeyIndex(), o.getKeyIndex());
167  }
168 
169  @Override
170  public int hashCode() {
171  int hash = 3;
172  return hash;
173  }
174 
175  @Override
176  public boolean equals(Object obj) {
177  if (this == obj) {
178  return true;
179  }
180  if (obj == null) {
181  return false;
182  }
183  if (getClass() != obj.getClass()) {
184  return false;
185  }
186  final OrderedKey other = (OrderedKey) obj;
187  if (this.keyIndex != other.keyIndex) {
188  return false;
189  }
190  return true;
191  }
192 
193  @Override
194  public String toString() {
195  // use toString on the key.
196  return this.getKeyValue() == null ? null : this.getKeyValue().toString();
197  }
198  }
199 
200  private static final long serialVersionUID = 1L;
201 
202  private static final Font DEFAULT_FONT = new JLabel().getFont();
203  private static final Font DEFAULT_HEADER_FONT = new Font(DEFAULT_FONT.getName(), DEFAULT_FONT.getStyle(), (int) (DEFAULT_FONT.getSize() * 1.5));
204 
205  private final ChartMessageOverlay overlay = new ChartMessageOverlay();
206  private final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
207  private final JFreeChart chart;
208  private final CategoryPlot plot;
209 
213  public BarChartPanel() {
214  this(null, null, null);
215  }
216 
224  public BarChartPanel(String title, String categoryLabel, String valueLabel) {
225  this.chart = ChartFactory.createStackedBarChart(
226  title,
227  categoryLabel,
228  valueLabel,
229  dataset,
230  PlotOrientation.VERTICAL,
231  true, false, false);
232 
233  // set style to match autopsy components
234  chart.setBackgroundPaint(null);
235  chart.getTitle().setFont(DEFAULT_HEADER_FONT);
236 
237  this.plot = ((CategoryPlot) chart.getPlot());
238  this.plot.getRenderer().setBaseItemLabelFont(DEFAULT_FONT);
239  plot.setBackgroundPaint(null);
240  plot.setOutlinePaint(null);
241 
242  // hide y axis labels
243  ValueAxis range = plot.getRangeAxis();
244  range.setVisible(false);
245 
246  // make sure x axis labels don't get cut off
247  plot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10);
248 
249  ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
250 
251  // Create Panel
252  ChartPanel panel = new ChartPanel(chart);
253  panel.addOverlay(overlay);
254  panel.setPopupMenu(null);
255 
256  this.setLayout(new BorderLayout());
257  this.add(panel, BorderLayout.CENTER);
258  }
259 
263  public String getTitle() {
264  return (this.chart == null || this.chart.getTitle() == null)
265  ? null
266  : this.chart.getTitle().getText();
267  }
268 
276  public BarChartPanel setTitle(String title) {
277  this.chart.getTitle().setText(title);
278  return this;
279  }
280 
281  @Override
282  protected void setMessage(boolean visible, String message) {
283  this.overlay.setVisible(visible);
284  this.overlay.setMessage(message);
285  }
286 
287  @Override
288  protected void setResults(List<BarChartPanel.BarChartSeries> data) {
289  this.dataset.clear();
290 
291  if (CollectionUtils.isNotEmpty(data)) {
292  for (int s = 0; s < data.size(); s++) {
293  BarChartPanel.BarChartSeries series = data.get(s);
294  if (series != null && CollectionUtils.isNotEmpty(series.getItems())) {
295  if (series.getColor() != null) {
296  this.plot.getRenderer().setSeriesPaint(s, series.getColor());
297  }
298 
299  for (int i = 0; i < series.getItems().size(); i++) {
300  BarChartItem bar = series.getItems().get(i);
301  this.dataset.setValue(bar.getValue(), series.getKey(), bar.getKey());
302  }
303  }
304  }
305  }
306  }
307 
308 }
BarChartSeries(Comparable<?> key, Color color, List< BarChartItem > items)
void setResults(List< BarChartPanel.BarChartSeries > data)
BarChartPanel(String title, String categoryLabel, String valueLabel)

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.