use of org.jfree.chart.axis.DateAxis in project AndrOBD by fr3ts0n.
the class ObdDataPlotter method createChart.
/**
* Creates a chart.
*
* @param dataset a dataset.
* @return A chart.
*/
private JFreeChart createChart(XYDataset dataset) {
chart = ChartFactory.createTimeSeriesChart(// title
"OBD Data Graph", // x-axis label
"Time", // y-axis label
"Value", // data
dataset, // create legend?
true, // generate tooltips?
true, // generate URLs?
false);
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
plot.getDomainAxis().setTickLabelFont(legendFont);
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));
chart.getLegend().setItemFont(legendFont);
return chart;
}
use of org.jfree.chart.axis.DateAxis in project spf4j by zolyfarkas.
the class Charts method createJFreeChart.
private static JFreeChart createJFreeChart(final String chartName, final String uom, final XYDataset timeseriescollection) {
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(chartName, "Time", uom, timeseriescollection, true, true, false);
XYPlot xyplot = (XYPlot) jfreechart.getPlot();
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
dateaxis.setVerticalTickLabels(true);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
xylineandshaperenderer.setBaseShapesVisible(true);
xylineandshaperenderer.setUseFillPaint(true);
xylineandshaperenderer.setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator("Tooltip {0}"));
return jfreechart;
}
use of org.jfree.chart.axis.DateAxis in project xwiki-platform by xwiki.
the class AxisConfigurator method setAxes.
/**
* Set the axes in the chart model.
*
* @param plotType The target plot type.
* @param chartModel The target chart model.
* @throws MacroExecutionException if the axes are incorrectly configured.
*/
public void setAxes(PlotType plotType, SimpleChartModel chartModel) throws MacroExecutionException {
AxisType[] defaultAxisTypes = plotType.getDefaultAxisTypes();
for (int i = 0; i < axisTypes.length; i++) {
AxisType type = axisTypes[i];
if (i >= defaultAxisTypes.length) {
if (type != null) {
throw new MacroExecutionException("To many axes for plot type.");
}
continue;
}
if (type == null) {
type = defaultAxisTypes[i];
}
Axis axis;
switch(type) {
case NUMBER:
NumberAxis numberAxis = new NumberAxis();
axis = numberAxis;
setNumberLimits(numberAxis, i);
break;
case CATEGORY:
axis = new CategoryAxis();
break;
case DATE:
DateAxis dateAxis = new DateAxis();
axis = dateAxis;
dateAxis.setTickMarkPosition(DateTickMarkPosition.END);
if (axisDateFormat[i] != null) {
try {
DateFormat dateFormat = new SimpleDateFormat(axisDateFormat[i], localeConfiguration.getLocale());
dateAxis.setDateFormatOverride(dateFormat);
} catch (IllegalArgumentException e) {
throw new MacroExecutionException(String.format("Invalid date format [%s].", axisDateFormat[i]));
}
}
setDateLimits(dateAxis, i);
break;
default:
throw new MacroExecutionException(String.format("Unsupported axis type [%s]", type.getName()));
}
chartModel.setAxis(i, axis);
}
}
use of org.jfree.chart.axis.DateAxis in project ma-core-public by infiniteautomation.
the class ImageChartUtils method writeChart.
public static void writeChart(PointTimeSeriesCollection pointTimeSeriesCollection, boolean showLegend, OutputStream out, int width, int height, long from, long to) throws IOException {
JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, null, showLegend, false, false);
chart.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.CHART_BACKGROUND_COLOUR));
XYPlot plot = chart.getXYPlot();
((DateAxis) plot.getDomainAxis()).setTimeZone(pointTimeSeriesCollection.getTimeZone());
plot.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.PLOT_BACKGROUND_COLOUR));
Color gridlines = SystemSettingsDao.getColour(SystemSettingsDao.PLOT_GRIDLINE_COLOUR);
plot.setDomainGridlinePaint(gridlines);
plot.setRangeGridlinePaint(gridlines);
((NumberAxis) plot.getRangeAxis()).setAutoRangeStickyZero(false);
double numericMin = 0;
double numericMax = 1;
int numericSeriesCount = pointTimeSeriesCollection.getNumericSeriesCount();
if (pointTimeSeriesCollection.hasNumericData()) {
for (int i = 0; i < numericSeriesCount; i++) {
NumericTimeSeries nts = pointTimeSeriesCollection.getNumericTimeSeries(i);
AbstractXYItemRenderer renderer;
if (nts.getPlotType() == DataPointVO.PlotTypes.STEP)
renderer = new XYStepRenderer();
else if (nts.getPlotType() == DataPointVO.PlotTypes.LINE)
renderer = new XYLineAndShapeRenderer(true, false);
else {
// XYCardinalSplineRenderer spline = new XYCardinalSplineRenderer(.5d, 16);
// XYSmoothLineAndShapeRenderer spline = new XYSmoothLineAndShapeRenderer();
// XYSplineRenderer spline = new XYSplineRenderer();
// spline.setBaseShapesVisible(false);
// renderer = spline;
renderer = new XYLineAndShapeRenderer(true, false);
}
if (nts.getPaint() != null)
renderer.setSeriesPaint(0, nts.getPaint(), false);
if (nts.getStroke() != null)
renderer.setSeriesStroke(0, nts.getStroke(), false);
plot.setDataset(i, new TimeSeriesCollection(nts.getTimeSeries()));
plot.setRenderer(i, renderer);
}
numericMin = plot.getRangeAxis().getLowerBound();
numericMax = plot.getRangeAxis().getUpperBound();
if (!pointTimeSeriesCollection.hasMultiplePoints()) {
// If this chart displays a single point, check if there should be a range description.
TimeSeries timeSeries = pointTimeSeriesCollection.getNumericTimeSeries(0).getTimeSeries();
String desc = timeSeries.getRangeDescription();
if (!StringUtils.isBlank(desc)) {
// Replace any HTML entities with Java equivalents
desc = StripEntities.stripHTMLEntities(desc, ' ');
plot.getRangeAxis().setLabel(desc);
}
}
} else
plot.getRangeAxis().setVisible(false);
if (pointTimeSeriesCollection.getRangeMarkers() != null) {
boolean rangeAdjusted = false;
for (Marker marker : pointTimeSeriesCollection.getRangeMarkers()) {
plot.addRangeMarker(marker);
if (marker instanceof ValueMarker) {
ValueMarker vm = (ValueMarker) marker;
if (numericMin > vm.getValue()) {
numericMin = vm.getValue();
rangeAdjusted = true;
}
if (numericMax < vm.getValue()) {
numericMax = vm.getValue();
rangeAdjusted = true;
}
}
}
if (rangeAdjusted) {
double adj = (numericMax - numericMin);
plot.getRangeAxis().setLowerBound(numericMin - adj * plot.getRangeAxis().getLowerMargin());
plot.getRangeAxis().setUpperBound(numericMax + adj * plot.getRangeAxis().getUpperMargin());
}
}
int discreteValueCount = pointTimeSeriesCollection.getDiscreteValueCount();
double interval = (numericMax - numericMin) / (discreteValueCount + 1);
int intervalIndex = 1;
if (pointTimeSeriesCollection.hasDiscreteData()) {
for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
XYStepRenderer renderer = new XYStepRenderer();
TimeSeries ts = new TimeSeries(dts.getName(), null, null);
for (IValueTime vt : dts.getValueTimes()) addMillisecond(ts, vt.getTime(), numericMin + (interval * (dts.getValueIndex(vt.getValue()) + intervalIndex)));
if (dts.getPaint() != null)
renderer.setSeriesPaint(0, dts.getPaint(), false);
if (dts.getStroke() != null)
renderer.setSeriesStroke(0, dts.getStroke(), false);
plot.setDataset(numericSeriesCount + i, new TimeSeriesCollection(ts, pointTimeSeriesCollection.getTimeZone()));
plot.setRenderer(numericSeriesCount + i, renderer);
intervalIndex += dts.getDiscreteValueCount();
}
}
if (from > 0)
plot.getDomainAxis().setLowerBound(from);
if (to > 0)
plot.getDomainAxis().setUpperBound(to);
if (pointTimeSeriesCollection.hasDiscreteData()) {
// Add the value annotations.
double annoX = plot.getDomainAxis().getLowerBound();
intervalIndex = 1;
for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
for (int j = 0; j < dts.getDiscreteValueCount(); j++) {
XYTextAnnotation anno = new XYTextAnnotation(" " + dts.getValueText(j), annoX, numericMin + (interval * (j + intervalIndex)));
if (!pointTimeSeriesCollection.hasNumericData() && intervalIndex + j == discreteValueCount)
// This prevents the top label from getting cut off
anno.setTextAnchor(TextAnchor.TOP_LEFT);
else
anno.setTextAnchor(TextAnchor.BOTTOM_LEFT);
anno.setPaint(((AbstractRenderer) plot.getRenderer(numericSeriesCount + i)).lookupSeriesPaint(0));
plot.addAnnotation(anno);
}
intervalIndex += dts.getDiscreteValueCount();
}
}
// Return the image.
ChartUtilities.writeChartAsPNG(out, chart, width, height);
}
use of org.jfree.chart.axis.DateAxis in project nimbus by nimbus-org.
the class DateAxisTickUnitAdjusterService method adjust.
// AbstractTickUnitAdjusterServiceのJavaDoc
protected void adjust(ValueAxis axis) {
if (!(axis instanceof DateAxis)) {
throw new IllegalArgumentException("axis is not DateAxis.");
}
DateAxis dateAxis = (DateAxis) axis;
DateTickUnit orgTickUnit = dateAxis.getTickUnit();
int unit = orgTickUnit.getUnit();
if (autoRangeMinimumSizeEnabled) {
double autoRangeMinimumSize = Double.NaN;
switch(unit) {
case DateTickUnit.MILLISECOND:
autoRangeMinimumSize = 1d;
break;
case DateTickUnit.SECOND:
autoRangeMinimumSize = 1000d;
break;
case DateTickUnit.MINUTE:
autoRangeMinimumSize = 60d * 1000d;
break;
case DateTickUnit.HOUR:
autoRangeMinimumSize = 60d * 60d * 1000d;
break;
case DateTickUnit.DAY:
autoRangeMinimumSize = 24d * 60d * 60d * 1000d;
break;
case DateTickUnit.MONTH:
autoRangeMinimumSize = 31d * 24d * 60d * 60d * 1000d;
break;
case DateTickUnit.YEAR:
autoRangeMinimumSize = 365d * 24d * 60d * 60d * 1000d;
break;
default:
break;
}
if (!Double.isNaN(autoRangeMinimumSize)) {
Plot plot = dateAxis.getPlot();
if (plot instanceof ValueAxisPlot) {
ValueAxisPlot vap = (ValueAxisPlot) plot;
Range r = vap.getDataRange(dateAxis);
if (r == null) {
if (zeroLength > 0.0d) {
dateAxis.setAutoRangeMinimumSize(zeroLength);
}
} else {
dateAxis.setAutoRangeMinimumSize(autoRangeMinimumSize * (Double.isNaN(unitCountCommonDivisor) ? 1.0d : unitCountCommonDivisor) * 2);
}
} else {
dateAxis.setAutoRangeMinimumSize(autoRangeMinimumSize * (Double.isNaN(unitCountCommonDivisor) ? 1.0d : unitCountCommonDivisor) * 2);
}
}
}
double length = dateAxis.getRange().getLength();
// ミリ秒以外は計算
switch(unit) {
case DateTickUnit.SECOND:
// 秒
length = length / 1000d;
break;
case DateTickUnit.MINUTE:
// 分
length = length / (60d * 1000d);
break;
case DateTickUnit.HOUR:
// 時
length = length / (60d * 60d * 1000d);
break;
case DateTickUnit.DAY:
// 日
length = length / (24d * 60d * 60d * 1000d);
break;
case DateTickUnit.MONTH:
// 月
length = length / (28d * 24d * 60d * 60d * 1000d);
break;
case DateTickUnit.YEAR:
// 年
length = length / (365d * 24d * 60d * 60d * 1000d);
break;
default:
break;
}
double unitCount = length / displayGraduationCount;
if (unitCount <= 0d) {
// 1ずつ表示
unitCount = 1d;
} else {
unitCount = adjustUnitCountByCommonDivisor(axis, unitCount);
}
DateFormat newFormat = null;
if (format != null) {
newFormat = format;
} else {
newFormat = new SimpleDateFormat();
}
int newUnitCount = (int) Math.ceil(unitCount);
// 新しいTickUnitを設定
dateAxis.setTickUnit(new DateTickUnit(unit, newUnitCount, newFormat));
}
Aggregations