Skip to content

Commit

Permalink
Merge pull request #2874 from SCADA-LTS/fix/#2823_Fixed_Consolidated_…
Browse files Browse the repository at this point in the history
…chart_not_showing_all_data_from_report2

#2823 fixed consolidated chart not showing all data from report:
  • Loading branch information
Limraj authored May 10, 2024
2 parents 5bba7ad + 96150f4 commit cf37559
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 97 deletions.
13 changes: 7 additions & 6 deletions src/com/serotonin/mango/vo/report/DiscreteTimeSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@
* @author Matthew Lohbihler
*/
public class DiscreteTimeSeries {
private final String name;
private final Comparable name;
private final TextRenderer textRenderer;
private final Paint paint;
private final List<PointValueTime> valueTimes = new ArrayList<PointValueTime>();
private final List<ValueDescription> valueDescriptions = new ArrayList<ValueDescription>();
private final List<PointValueTime> valueTimes = new ArrayList<>();
private final List<ValueDescription> valueDescriptions = new ArrayList<>();

public DiscreteTimeSeries(String name, TextRenderer textRenderer) {
@Deprecated(since = "2.7.8")
public DiscreteTimeSeries(Comparable name, TextRenderer textRenderer) {
this(name, textRenderer, null);
}

public DiscreteTimeSeries(String name, TextRenderer textRenderer, Paint paint) {
public DiscreteTimeSeries(Comparable name, TextRenderer textRenderer, Paint paint) {
this.name = name;
this.textRenderer = textRenderer;
this.paint = paint;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void addValueTime(PointValueTime pvt) {
}
}

public String getName() {
public Comparable getName() {
return name;
}

Expand Down
81 changes: 44 additions & 37 deletions src/com/serotonin/mango/vo/report/ImageChartUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,25 @@
*/
package com.serotonin.mango.vo.report;

import java.awt.Color;
import java.awt.Paint;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.*;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
Expand All @@ -56,7 +54,9 @@
/**
* @author Matthew Lohbihler
*/
public class ImageChartUtils {
public final class ImageChartUtils {

private ImageChartUtils() {}

private static final Log LOG = LogFactory.getLog(ImageChartUtils.class);

Expand Down Expand Up @@ -99,12 +99,11 @@ public static void writeChart(PointTimeSeriesCollection pointTimeSeriesCollectio
double numericMin = 0;
double numericMax = 1;
if (pointTimeSeriesCollection.hasNumericData()) {
// XYSplineRenderer numericRenderer = new XYSplineRenderer();
// numericRenderer.setBaseShapesVisible(false);
XYLineAndShapeRenderer numericRenderer = new XYLineAndShapeRenderer(true, false);
TimeSeriesCollection timeSeriesCollection = pointTimeSeriesCollection.getNumericTimeSeriesCollection();

plot.setDataset(NUMERIC_DATA_INDEX, pointTimeSeriesCollection.getNumericTimeSeriesCollection());
plot.setRenderer(NUMERIC_DATA_INDEX, numericRenderer);
plot.setDataset(NUMERIC_DATA_INDEX, timeSeriesCollection);

for (int i = 0; i < pointTimeSeriesCollection.getNumericPaint().size(); i++) {
Paint paint = pointTimeSeriesCollection.getNumericPaint().get(i);
Expand All @@ -131,20 +130,20 @@ public static void writeChart(PointTimeSeriesCollection pointTimeSeriesCollectio

if (pointTimeSeriesCollection.hasDiscreteData()) {
XYStepRenderer discreteRenderer = new XYStepRenderer();
TimeSeriesCollection timeSeriesCollection = pointTimeSeriesCollection.createTimeSeriesCollection(numericMin, numericMax);

plot.setRenderer(DISCRETE_DATA_INDEX, discreteRenderer, false);
plot.setDataset(DISCRETE_DATA_INDEX, timeSeriesCollection);

// Plot the data
int discreteValueCount = pointTimeSeriesCollection.getDiscreteValueCount();
double interval = (numericMax - numericMin) / (discreteValueCount + 1);

TimeSeriesCollection timeSeriesCollection = pointTimeSeriesCollection.createTimeSeriesCollection(numericMin, interval);

plot.setDataset(DISCRETE_DATA_INDEX, timeSeriesCollection);
int discreteSeriesCount = pointTimeSeriesCollection.getDiscreteSeriesCount();
double interval = pointTimeSeriesCollection.getDiscreteInterval(numericMin, numericMax);

// Add the value annotations.
double annoX = plot.getDomainAxis().getLowerBound();
int intervalIndex = 1;
for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
for (int i = 0; i < discreteSeriesCount; i++) {
DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
if (dts.getPaint() != null)
discreteRenderer.setSeriesPaint(i, dts.getPaint());
Expand All @@ -165,30 +164,17 @@ public static void writeChart(PointTimeSeriesCollection pointTimeSeriesCollectio
}
}

if(showLegend && chart.getLegend() != null) {
LegendItemCollection legendItemCollection = getLegendItemCollectionSort(chart);
if(legendItemCollection != null)
plot.setFixedLegendItems(legendItemCollection);
}


// Return the image.
ChartUtilities.writeChartAsPNG(out, chart, width, height);
}

// public static void writeChart(TimeSeries timeSeries, OutputStream out, int width, int height) throws IOException
// {
// writeChart(new TimeSeriesCollection(timeSeries), false, out, width, height);
// }
//
// public static void writeChart(TimeSeriesCollection timeSeriesCollection, boolean showLegend, OutputStream out,
// int width, int height) throws IOException {
// JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, timeSeriesCollection, showLegend,
// false, false);
// chart.setBackgroundPaint(Color.white);
//
// // Change the plot renderer
// // XYPlot plot = chart.getXYPlot();
// // XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
// // plot.setRenderer(renderer);
//
// // Return the image.
// ChartUtilities.writeChartAsPNG(out, chart, width, height);
// }

public static void writeChart(HttpServletResponse response, byte[] chartData) throws IOException {
response.setContentType(getContentType());
StreamUtils.transfer(new ByteArrayInputStream(chartData), response.getOutputStream());
Expand Down Expand Up @@ -246,6 +232,27 @@ public static String calculatePointNameForReport(String extendedName) {
}

private static List<String> toListNames(List<ReportChartCreator.PointStatistics> pointStatistics) {
return pointStatistics.stream().map(ReportChartCreator.PointStatistics::getName).collect(Collectors.toList());
return pointStatistics.stream().map(a -> a.getName().toString()).collect(Collectors.toList());
}

private static LegendItemCollection getLegendItemCollectionSort(JFreeChart chart) {
LegendTitle legend = chart.getLegend();
LegendItemSource[] legendItemSource = legend.getSources();
if(legendItemSource.length > 0) {
LegendItemCollection legendItemCollections = legendItemSource[0].getLegendItems();
Iterator iterator = legendItemCollections.iterator();
List<LegendItem> legendItems = new ArrayList<>();
while (iterator.hasNext()) {
LegendItem legendItem = (LegendItem) iterator.next();
legendItems.add(legendItem);
}
legendItems.sort(Comparator.comparing(LegendItem::getSeriesKey));
LegendItemCollection legendItemCollection = new LegendItemCollection();
for (LegendItem legendItem : legendItems) {
legendItemCollection.add(legendItem);
}
return legendItemCollection;
}
return null;
}
}
87 changes: 51 additions & 36 deletions src/com/serotonin/mango/vo/report/PointTimeSeriesCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
package com.serotonin.mango.vo.report;

import java.awt.Paint;
import java.util.ArrayList;

import java.util.Comparator;
import java.util.List;
import java.util.*;

import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
Expand All @@ -34,78 +31,83 @@
* @author Matthew Lohbihler
*/
public class PointTimeSeriesCollection {
private TimeSeriesCollection numericTimeSeriesCollection;
private List<Paint> numericPaint;
private List<DiscreteTimeSeries> discreteTimeSeriesCollection;

public void addNumericTimeSeries(TimeSeries numericTimeSeries) {
addNumericTimeSeries(numericTimeSeries, null);
private Map<Comparable, TimeSeries> numericTimeSeriesCollection;
private Map<Comparable, DiscreteTimeSeries> discreteTimeSeriesCollection;
private Map<Comparable, Paint> numericPaint;

public PointTimeSeriesCollection() {
this.numericTimeSeriesCollection = new TreeMap<>();
this.discreteTimeSeriesCollection = new TreeMap<>();
this.numericPaint = new TreeMap<>();
}

public void addNumericTimeSeries(TimeSeries numericTimeSeries, Paint paint) {
if (numericTimeSeriesCollection == null) {
numericTimeSeriesCollection = new TimeSeriesCollection();
numericPaint = new ArrayList<Paint>();
}
numericTimeSeriesCollection.addSeries(numericTimeSeries);
numericPaint.add(paint);
@Deprecated(since = "2.7.8")
public void addNumericTimeSeries(TimeSeries timeSeries) {
addNumericTimeSeries(timeSeries, null);
}

public void addNumericTimeSeries(TimeSeries timeSeries, Paint paint) {
numericTimeSeriesCollection.put(timeSeries.getKey(), timeSeries);
numericPaint.put(timeSeries.getKey(), paint);
}

public void addDiscreteTimeSeries(DiscreteTimeSeries discreteTimeSeries) {
if (discreteTimeSeriesCollection == null)
discreteTimeSeriesCollection = new ArrayList<>();
discreteTimeSeriesCollection.add(discreteTimeSeries);
discreteTimeSeriesCollection.sort(Comparator.comparing(DiscreteTimeSeries::getName));
discreteTimeSeriesCollection.put(discreteTimeSeries.getName(), discreteTimeSeries);
}

public boolean hasData() {
return hasNumericData() || hasDiscreteData();
}

public boolean hasNumericData() {
return numericTimeSeriesCollection != null;
return !numericTimeSeriesCollection.isEmpty();
}

public boolean hasDiscreteData() {
return discreteTimeSeriesCollection != null;
return !discreteTimeSeriesCollection.isEmpty();
}

public boolean hasMultiplePoints() {
int count = 0;
if (numericTimeSeriesCollection != null)
count += numericTimeSeriesCollection.getSeriesCount();
count += numericTimeSeriesCollection.size();
if (discreteTimeSeriesCollection != null)
count += discreteTimeSeriesCollection.size();
return count > 1;
}

public TimeSeriesCollection getNumericTimeSeriesCollection() {
return numericTimeSeriesCollection;
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
for(TimeSeries timeSeries: getTimeSeriesCollection()) {
timeSeriesCollection.addSeries(timeSeries);
}
return timeSeriesCollection;
}

public List<Paint> getNumericPaint() {
return numericPaint;
return new ArrayList<>(numericPaint.values());
}

public int getDiscreteValueCount() {
int count = 0;

if (discreteTimeSeriesCollection != null) {
for (DiscreteTimeSeries dts : discreteTimeSeriesCollection)
count += dts.getDiscreteValueCount();
}

for (DiscreteTimeSeries dts : getDiscreteTimeSeriesCollection())
count += dts.getDiscreteValueCount();


return count;
}

public TimeSeriesCollection createTimeSeriesCollection(double numericMin, double spacingInterval) {
public TimeSeriesCollection createTimeSeriesCollection(double numericMin, double numericMax) {

double spacingInterval = getDiscreteInterval(numericMin, numericMax);
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();

int intervalIndex = 1;
int index = 0;
for (DiscreteTimeSeries dts : discreteTimeSeriesCollection) {
SeriesIdentifier seriesIdentifier = new SeriesIdentifier(++index, dts.getName());
TimeSeries ts = new TimeSeries(seriesIdentifier, null, null, Second.class);
for (DiscreteTimeSeries dts : getDiscreteTimeSeriesCollection()) {
TimeSeries ts = new TimeSeries(dts.getName(), null, null, Second.class);

for (PointValueTime pvt : dts.getValueTimes())
ImageChartUtils.addSecond(ts, pvt.getTime(), numericMin
Expand All @@ -120,10 +122,23 @@ public TimeSeriesCollection createTimeSeriesCollection(double numericMin, double
}

public int getDiscreteSeriesCount() {
return discreteTimeSeriesCollection.size();
return getDiscreteTimeSeriesCollection().size();
}

public DiscreteTimeSeries getDiscreteTimeSeries(int index) {
return discreteTimeSeriesCollection.get(index);
return getDiscreteTimeSeriesCollection().get(index);
}

public double getDiscreteInterval(double numericMin, double numericMax) {
int discreteValueCount = getDiscreteValueCount();
return (numericMax - numericMin) / (discreteValueCount + 1);
}

private List<DiscreteTimeSeries> getDiscreteTimeSeriesCollection() {
return new ArrayList<>(discreteTimeSeriesCollection.values());
}

private List<TimeSeries> getTimeSeriesCollection() {
return new ArrayList<>(numericTimeSeriesCollection.values());
}
}
Loading

0 comments on commit cf37559

Please sign in to comment.