Autopsy  4.19.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.Font;
23 import java.util.List;
24 import javax.swing.JLabel;
25 import org.apache.commons.collections4.CollectionUtils;
26 import org.jfree.chart.ChartFactory;
27 import org.jfree.chart.ChartPanel;
28 import org.jfree.chart.JFreeChart;
29 import org.jfree.chart.axis.ValueAxis;
30 import org.jfree.chart.plot.CategoryPlot;
31 import org.jfree.chart.plot.PlotOrientation;
32 import org.jfree.chart.renderer.category.BarRenderer;
33 import org.jfree.chart.renderer.category.StandardBarPainter;
34 import org.jfree.data.category.DefaultCategoryDataset;
36 
40 public class BarChartPanel extends AbstractLoadableComponent<List<BarChartSeries>> {
41 
47  public static class OrderedKey implements Comparable<OrderedKey> {
48 
49  private final Object keyValue;
50  private final int keyIndex;
51 
59  public OrderedKey(Object keyValue, int keyIndex) {
60  this.keyValue = keyValue;
61  this.keyIndex = keyIndex;
62  }
63 
67  Object getKeyValue() {
68  return keyValue;
69  }
70 
74  int getKeyIndex() {
75  return keyIndex;
76  }
77 
78  @Override
79  public int compareTo(OrderedKey o) {
80  // this will have a higher value than null.
81  if (o == null) {
82  return 1;
83  }
84 
85  // compare by index
86  return Integer.compare(this.getKeyIndex(), o.getKeyIndex());
87  }
88 
89  @Override
90  public int hashCode() {
91  int hash = 3;
92  return hash;
93  }
94 
95  @Override
96  public boolean equals(Object obj) {
97  if (this == obj) {
98  return true;
99  }
100  if (obj == null) {
101  return false;
102  }
103  if (getClass() != obj.getClass()) {
104  return false;
105  }
106  final OrderedKey other = (OrderedKey) obj;
107  if (this.keyIndex != other.keyIndex) {
108  return false;
109  }
110  return true;
111  }
112 
113  @Override
114  public String toString() {
115  // use toString on the key.
116  return this.getKeyValue() == null ? null : this.getKeyValue().toString();
117  }
118  }
119 
120  private static final long serialVersionUID = 1L;
121 
122  private static final Font DEFAULT_FONT = new JLabel().getFont();
123  private static final Font DEFAULT_HEADER_FONT = new Font(DEFAULT_FONT.getName(), DEFAULT_FONT.getStyle(), (int) (DEFAULT_FONT.getSize() * 1.5));
124 
125  private final ChartMessageOverlay overlay = new ChartMessageOverlay();
126  private final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
127  private final JFreeChart chart;
128  private final CategoryPlot plot;
129 
133  public BarChartPanel() {
134  this(null, null, null);
135  }
136 
144  public BarChartPanel(String title, String categoryLabel, String valueLabel) {
145  this.chart = ChartFactory.createStackedBarChart(
146  title,
147  categoryLabel,
148  valueLabel,
149  dataset,
150  PlotOrientation.VERTICAL,
151  true, false, false);
152 
153  // set style to match autopsy components
154  chart.setBackgroundPaint(null);
155  chart.getTitle().setFont(DEFAULT_HEADER_FONT);
156 
157  this.plot = ((CategoryPlot) chart.getPlot());
158  this.plot.getRenderer().setBaseItemLabelFont(DEFAULT_FONT);
159  plot.setBackgroundPaint(null);
160  plot.setOutlinePaint(null);
161 
162  // hide y axis labels
163  ValueAxis range = plot.getRangeAxis();
164  range.setVisible(false);
165 
166  // make sure x axis labels don't get cut off
167  plot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10);
168 
169  ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
170 
171  // Create Panel
172  ChartPanel panel = new ChartPanel(chart);
173  panel.addOverlay(overlay);
174  panel.setPopupMenu(null);
175 
176  this.setLayout(new BorderLayout());
177  this.add(panel, BorderLayout.CENTER);
178  }
179 
183  public String getTitle() {
184  return (this.chart == null || this.chart.getTitle() == null)
185  ? null
186  : this.chart.getTitle().getText();
187  }
188 
196  public BarChartPanel setTitle(String title) {
197  this.chart.getTitle().setText(title);
198  return this;
199  }
200 
201  @Override
202  protected void setMessage(boolean visible, String message) {
203  this.overlay.setVisible(visible);
204  this.overlay.setMessage(message);
205  }
206 
207  @Override
208  protected void setResults(List<BarChartSeries> data) {
209  this.dataset.clear();
210 
211  if (CollectionUtils.isNotEmpty(data)) {
212  for (int s = 0; s < data.size(); s++) {
213  BarChartSeries series = data.get(s);
214  if (series != null && CollectionUtils.isNotEmpty(series.getItems())) {
215  if (series.getColor() != null) {
216  this.plot.getRenderer().setSeriesPaint(s, series.getColor());
217  }
218 
219  for (int i = 0; i < series.getItems().size(); i++) {
220  BarChartItem bar = series.getItems().get(i);
221  this.dataset.setValue(bar.getValue(), series.getKey(), bar.getKey());
222  }
223  }
224  }
225  }
226  }
227 
228 }
BarChartPanel(String title, String categoryLabel, String valueLabel)

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.