Autopsy  4.20.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 
42  private static final long serialVersionUID = 1L;
43 
44  private static final Font DEFAULT_FONT = new JLabel().getFont();
45  private static final Font DEFAULT_HEADER_FONT = new Font(DEFAULT_FONT.getName(), DEFAULT_FONT.getStyle(), (int) (DEFAULT_FONT.getSize() * 1.5));
46 
47  private final ChartMessageOverlay overlay = new ChartMessageOverlay();
48  private final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
49  private final JFreeChart chart;
50  private final CategoryPlot plot;
51 
55  public BarChartPanel() {
56  this(null, null, null);
57  }
58 
66  public BarChartPanel(String title, String categoryLabel, String valueLabel) {
67  this.chart = ChartFactory.createStackedBarChart(
68  title,
69  categoryLabel,
70  valueLabel,
71  dataset,
72  PlotOrientation.VERTICAL,
73  true, false, false);
74 
75  // set style to match autopsy components
76  chart.setBackgroundPaint(null);
77  chart.getTitle().setFont(DEFAULT_HEADER_FONT);
78 
79  this.plot = ((CategoryPlot) chart.getPlot());
80  this.plot.getRenderer().setDefaultItemLabelFont(DEFAULT_FONT);
81  plot.setBackgroundPaint(null);
82  plot.setOutlinePaint(null);
83 
84  // hide y axis labels
85  ValueAxis range = plot.getRangeAxis();
86  range.setVisible(false);
87 
88  // make sure x axis labels don't get cut off
89  plot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10);
90 
91  ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
92 
93  // Create Panel
94  ChartPanel panel = new ChartPanel(chart);
95  panel.addOverlay(overlay);
96  panel.setPopupMenu(null);
97 
98  this.setLayout(new BorderLayout());
99  this.add(panel, BorderLayout.CENTER);
100  }
101 
105  public String getTitle() {
106  return (this.chart == null || this.chart.getTitle() == null)
107  ? null
108  : this.chart.getTitle().getText();
109  }
110 
118  public BarChartPanel setTitle(String title) {
119  this.chart.getTitle().setText(title);
120  return this;
121  }
122 
123  @Override
124  protected void setMessage(boolean visible, String message) {
125  this.overlay.setVisible(visible);
126  this.overlay.setMessage(message);
127  }
128 
129  @Override
130  protected void setResults(List<BarChartSeries> data) {
131  this.dataset.clear();
132 
133  if (CollectionUtils.isNotEmpty(data)) {
134  for (int s = 0; s < data.size(); s++) {
135  BarChartSeries series = data.get(s);
136  if (series != null && CollectionUtils.isNotEmpty(series.getItems())) {
137  if (series.getColor() != null) {
138  this.plot.getRenderer().setSeriesPaint(s, series.getColor());
139  }
140 
141  for (int i = 0; i < series.getItems().size(); i++) {
142  BarChartItem bar = series.getItems().get(i);
143  this.dataset.setValue(bar.getValue(), series.getKey(), bar.getKey());
144  }
145  }
146  }
147  }
148  }
149 
150 }
BarChartPanel(String title, String categoryLabel, String valueLabel)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.