Parent: [a7a9ff] (diff)

Download this file

SensorDataTrackView.java    149 lines (128 with data), 5.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.JLabel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import squirrel.model.ModelFacade;
import squirrel.model.io.DataColumn;
import squirrel.model.io.DataSource;
import squirrel.util.Range;
@SuppressWarnings("serial")
public class SensorDataTrackView extends TrackView {
public enum RangeAxisType {
NUMBER, DATE;
}
private SensorDataFilterAdapter currentDataset;
private JLabel lblNoData;
private JFreeChart chart;
public ChartPanel chartPanel;
private RangeAxisType rangeAxisType;
private SensorDataTrackView(ModelFacade model, int index, Range startRange,
RangeAxisType rangeAxisType) {
super(model, index, startRange);
model.setFilterSize((int) (3 * startRange.getDistance()));
DataSource<? extends DataColumn> source = model.getCurrentDataSource();
this.name = source.getDataColumn(index).getName();
this.rangeAxisType = rangeAxisType;
this.currentDataset = new SensorDataFilterAdapter(model, source.toSensorIndex(index), startRange);
this.setVisibleExcerpt(startRange);
initGUI();
chartPanel.setFocusable(true);
}
public SensorDataTrackView(ModelFacade model, int index, Range startRange) {
this(model, index, startRange, RangeAxisType.NUMBER);
}
private void initGUI() {
this.setLayout(new BorderLayout());
this.chart = createChart();
this.chartPanel = createChartPanel(chart);
this.add(chartPanel, BorderLayout.CENTER);
}
private JFreeChart createChart() {
chart = ChartFactory.
createXYLineChart(name,
"time",
"amplitude",
currentDataset,
PlotOrientation.VERTICAL,
false, // no legend
false, // no tooltips
false); // no urls
chart.setTitle((String) null);
chart.setAntiAlias(false);
chart.setBorderVisible(false);
chart.setBackgroundPaint(null);
return chart;
}
private ChartPanel createChartPanel(JFreeChart chart) {
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setDisplayToolTips(false);
chartPanel.setMouseZoomable(false);
chartPanel.setPopupMenu(null);
XYPlot plot = chart.getXYPlot();
plot.getRenderer().setBasePaint(Color.ORANGE);
plot.getRenderer().setSeriesPaint(0, Color.ORANGE);
ValueAxis rangeAxis = plot.getRangeAxis();
ValueAxis domainAxis = plot.getDomainAxis();
rangeAxis.setLowerMargin(0.02);
rangeAxis.setUpperMargin(0.02);
rangeAxis.setLabel(null);
domainAxis.setLabel(null);
domainAxis.setLowerMargin(0);
domainAxis.setUpperMargin(0);
chartPanel.setMinimumDrawWidth(0);
chartPanel.setMaximumDrawWidth(2500);
chartPanel.setMinimumDrawHeight(0);
chartPanel.setMaximumDrawHeight(2500);
return chartPanel;
}
Rectangle getPlotBounds() {
Rectangle ret = new Rectangle(0, 0, 0, 0);
EntityCollection ec = chartPanel.getChartRenderingInfo().getEntityCollection();
if (ec.getEntities().size() > 1) {
ret = (Rectangle) ec.getEntity(1).getArea();
}
return ret;
}
@Override
public void setVisibleExcerpt(Range range) {
super.setVisibleExcerpt(range);
this.currentDataset.setVisibleExcerpt(range);
}
@Override
public long translatePixelToTimestamp(int pixel) {
Rectangle bounds = getPlotBounds();
return (long) ((double) ((pixel - bounds.getX()) / bounds.getWidth() * visibleExcerpt.getDistance())) + visibleExcerpt.getStart();
}
@Override
public int translateTimestampToPixel(long timestamp) {
Rectangle bounds = getPlotBounds();
long distance = visibleExcerpt.getDistance();
return (int) ((double) (timestamp - visibleExcerpt.getStart()) / (double) distance * bounds.getWidth()) + (int) bounds.getX();
}
}