use of eu.hansolo.tilesfx.chart.ChartData in project tilesfx by HanSolo.
the class RadarChartTileSkin method initGraphics.
// ******************** Initialization ************************************
@Override
protected void initGraphics() {
super.initGraphics();
radarChart = new RadarChart(tile.getChartData());
radarChart.setMaxValue(tile.getMaxValue());
radarChart.setUnit(tile.getUnit());
radarChart.setLegendVisible(true);
radarChart.setThresholdVisible(tile.isThresholdVisible());
radarChart.setMode(tile.getRadarChartMode());
radarChart.setGridColor(tile.getChartGridColor());
radarChart.setChartTextColor(tile.getTextColor());
radarChart.setThresholdColor(tile.getThresholdColor());
radarChart.setGradientStops(tile.getGradientStops());
chartEventListener = e -> radarChart.redraw();
tile.getChartData().forEach(chartData -> chartData.addChartDataEventListener(chartEventListener));
chartDataListener = c -> {
while (c.next()) {
if (c.wasAdded()) {
c.getAddedSubList().forEach(addedItem -> addedItem.addChartDataEventListener(chartEventListener));
} else if (c.wasRemoved()) {
c.getRemoved().forEach(removedItem -> removedItem.removeChartDataEventListener(chartEventListener));
}
}
radarChart.redraw();
};
titleText = new Text();
titleText.setFill(tile.getTitleColor());
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
text = new Text(tile.getText());
text.setFill(tile.getTextColor());
Helper.enableNode(text, tile.isTextVisible());
getPane().getChildren().addAll(titleText, radarChart, text);
}
use of eu.hansolo.tilesfx.chart.ChartData in project tilesfx by HanSolo.
the class RadialChartTileSkin method drawChart.
private void drawChart() {
double canvasSize = chartCanvas.getWidth();
double radius = canvasSize * 0.5;
double innerSpacer = radius * 0.18;
double barWidth = (radius - innerSpacer) / tile.getChartData().size();
// List<RadialChartData> sortedDataList = tile.getChartData().stream().sorted(Comparator.comparingDouble(RadialChartData::getValue)).collect(Collectors.toList());
List<ChartData> dataList = tile.getChartData();
int noOfItems = dataList.size();
double max = noOfItems == 0 ? 0 : dataList.stream().max(Comparator.comparingDouble(ChartData::getValue)).get().getValue();
double nameX = radius * 0.975;
double nameWidth = radius * 0.95;
double valueY = radius * 0.94;
double valueWidth = barWidth * 0.9;
Color bkgColor = Color.color(tile.getTextColor().getRed(), tile.getTextColor().getGreen(), tile.getTextColor().getBlue(), 0.15);
chartCtx.clearRect(0, 0, canvasSize, canvasSize);
chartCtx.setLineCap(StrokeLineCap.BUTT);
chartCtx.setFill(tile.getTextColor());
chartCtx.setTextAlign(TextAlignment.RIGHT);
chartCtx.setTextBaseline(VPos.CENTER);
chartCtx.setFont(Fonts.latoRegular(barWidth * 0.5));
chartCtx.setStroke(bkgColor);
chartCtx.setLineWidth(1);
chartCtx.strokeLine(radius, 0, radius, radius - barWidth * 0.875);
chartCtx.strokeLine(0, radius, radius - barWidth * 0.875, radius);
chartCtx.strokeArc(noOfItems * barWidth, noOfItems * barWidth, canvasSize - (2 * noOfItems * barWidth), canvasSize - (2 * noOfItems * barWidth), 90, -270, ArcType.OPEN);
for (int i = 0; i < noOfItems; i++) {
ChartData data = dataList.get(i);
double value = clamp(0, Double.MAX_VALUE, data.getValue());
double bkgXY = i * barWidth;
double bkgWH = canvasSize - (2 * i * barWidth);
double barXY = barWidth * 0.5 + i * barWidth;
double barWH = canvasSize - barWidth - (2 * i * barWidth);
double angle = value / max * 270.0;
// Background
chartCtx.setLineWidth(1);
chartCtx.setStroke(bkgColor);
chartCtx.strokeArc(bkgXY, bkgXY, bkgWH, bkgWH, 90, -270, ArcType.OPEN);
// DataBar
chartCtx.setLineWidth(barWidth);
chartCtx.setStroke(data.getFillColor());
chartCtx.strokeArc(barXY, barXY, barWH, barWH, 90, -angle, ArcType.OPEN);
// Name
chartCtx.setTextAlign(TextAlignment.RIGHT);
chartCtx.fillText(data.getName(), nameX, barXY, nameWidth);
// Value
chartCtx.setTextAlign(TextAlignment.CENTER);
chartCtx.fillText(String.format(Locale.US, "%.0f", value), barXY, valueY, valueWidth);
}
}
use of eu.hansolo.tilesfx.chart.ChartData in project tilesfx by HanSolo.
the class RadialChartTileSkin method initGraphics.
// ******************** Initialization ************************************
@Override
protected void initGraphics() {
super.initGraphics();
chartEventListener = e -> drawChart();
tile.getChartData().forEach(chartData -> chartData.addChartDataEventListener(chartEventListener));
chartDataListener = c -> {
while (c.next()) {
if (c.wasAdded()) {
c.getAddedSubList().forEach(addedItem -> addedItem.addChartDataEventListener(chartEventListener));
} else if (c.wasRemoved()) {
c.getRemoved().forEach(removedItem -> removedItem.removeChartDataEventListener(chartEventListener));
}
}
drawChart();
};
clickHandler = e -> {
double x = e.getX();
double y = e.getY();
double startAngle = 90;
List<ChartData> dataList = tile.getChartData();
int noOfItems = dataList.size();
double canvasSize = chartCanvas.getWidth();
double barWidth = canvasSize * 0.1;
double max = noOfItems == 0 ? 0 : dataList.stream().max(Comparator.comparingDouble(ChartData::getValue)).get().getValue();
for (int i = 0; i < noOfItems; i++) {
ChartData data = dataList.get(i);
double value = clamp(0, Double.MAX_VALUE, data.getValue());
double barXY = barWidth * 0.5 + i * barWidth;
double barWH = canvasSize - barWidth - (2 * i * barWidth);
double angle = value / max * 270.0;
double centerX = barXY + barWH * 0.5;
double centerY = centerX;
boolean hit = Helper.isInRingSegment(x, y, centerX, centerY, (barWH + barWidth) * 0.5, (barWH - barWidth) * 0.5, startAngle, angle);
if (hit) {
tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, data));
break;
}
}
};
titleText = new Text();
titleText.setFill(tile.getTitleColor());
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
text = new Text(tile.getText());
text.setFill(tile.getTextColor());
Helper.enableNode(text, tile.isTextVisible());
chartCanvas = new Canvas(size * 0.9, tile.isTextVisible() ? size * 0.72 : size * 0.795);
chartCtx = chartCanvas.getGraphicsContext2D();
getPane().getChildren().addAll(titleText, chartCanvas, text);
}
use of eu.hansolo.tilesfx.chart.ChartData in project tilesfx by HanSolo.
the class RadialPercentageTileSkin method initGraphics.
// ******************** Initialization ************************************
@Override
protected void initGraphics() {
super.initGraphics();
if (tile.isAutoScale())
tile.calcAutoScale();
minValue = tile.getMinValue();
range = tile.getRange();
sectionsVisible = tile.getSectionsVisible();
sections = tile.getSections();
formatString = new StringBuilder("%.").append(Integer.toString(tile.getDecimals())).append("f").toString();
locale = tile.getLocale();
dataList = tile.getChartData();
sum = dataList.stream().mapToDouble(ChartData::getValue).sum();
angleStep = ANGLE_RANGE / sum;
referenceValue = tile.getReferenceValue() < maxValue ? maxValue : tile.getReferenceValue();
chartEventListener = e -> setProportionBar();
tile.getChartData().forEach(chartData -> chartData.addChartDataEventListener(chartEventListener));
chartDataListener = c -> {
dataList = tile.getChartData();
sum = dataList.stream().mapToDouble(ChartData::getValue).sum();
setProportionBar();
};
currentValueListener = o -> setBar(tile.getCurrentValue());
titleText = new Text();
titleText.setFill(tile.getTitleColor());
enableNode(titleText, !tile.getTitle().isEmpty());
text = new Text(tile.getText());
text.setFill(tile.getTextColor());
enableNode(text, tile.isTextVisible());
barBackground = new Arc(PREFERRED_WIDTH * 0.5, PREFERRED_HEIGHT * 0.5, PREFERRED_WIDTH * 0.468, PREFERRED_HEIGHT * 0.468, 90, 360);
barBackground.setType(ArcType.OPEN);
barBackground.setStroke(tile.getBarBackgroundColor());
barBackground.setStrokeWidth(PREFERRED_WIDTH * 0.1);
barBackground.setStrokeLineCap(StrokeLineCap.BUTT);
barBackground.setFill(null);
bar = new Arc(PREFERRED_WIDTH * 0.5, PREFERRED_HEIGHT * 0.5, PREFERRED_WIDTH * 0.468, PREFERRED_HEIGHT * 0.468, 90, 0);
bar.setType(ArcType.OPEN);
bar.setStroke(tile.getBarColor());
bar.setStrokeWidth(PREFERRED_WIDTH * 0.1);
bar.setStrokeLineCap(StrokeLineCap.BUTT);
bar.setFill(null);
proportionBar = new Arc(PREFERRED_WIDTH * 0.5, PREFERRED_HEIGHT * 0.5, PREFERRED_WIDTH * 0.468, PREFERRED_HEIGHT * 0.468, 90, 0);
proportionBar.setType(ArcType.OPEN);
proportionBar.setStroke(tile.getBarColor());
proportionBar.setStrokeWidth(PREFERRED_WIDTH * 0.015);
proportionBar.setStrokeLineCap(StrokeLineCap.BUTT);
proportionBar.setFill(null);
separator = new Line(PREFERRED_WIDTH * 0.5, 1, PREFERRED_WIDTH * 0.5, 0.16667 * PREFERRED_HEIGHT);
separator.setStroke(tile.getBackgroundColor());
separator.setFill(Color.TRANSPARENT);
percentageValueText = new Text(String.format(locale, formatString, tile.getCurrentValue()));
percentageValueText.setFont(Fonts.latoRegular(PREFERRED_WIDTH * 0.27333));
percentageValueText.setFill(tile.getValueColor());
percentageValueText.setTextOrigin(VPos.CENTER);
percentageUnitText = new Text(tile.getUnit());
percentageUnitText = new Text("\u0025");
percentageUnitText.setFont(Fonts.latoLight(PREFERRED_WIDTH * 0.08));
percentageUnitText.setFill(tile.getUnitColor());
percentageFlow = new TextFlow(percentageValueText, percentageUnitText);
percentageFlow.setTextAlignment(TextAlignment.CENTER);
descriptionText = new Text(String.format(locale, formatString, tile.getCurrentValue()));
descriptionText.setFont(Fonts.latoRegular(PREFERRED_WIDTH * 0.27333));
descriptionText.setFill(tile.getValueColor());
descriptionText.setTextOrigin(VPos.CENTER);
enableNode(descriptionText, tile.isValueVisible());
unitText = new Text(tile.getUnit());
unitText = new Text("\u0025");
unitText.setFont(Fonts.latoLight(PREFERRED_WIDTH * 0.08));
unitText.setFill(tile.getUnitColor());
enableNode(unitText, !tile.getUnit().isEmpty());
valueUnitFlow = new TextFlow(descriptionText, unitText);
valueUnitFlow.setTextAlignment(TextAlignment.CENTER);
getPane().getChildren().addAll(barBackground, proportionBar, bar, separator, titleText, text, percentageFlow, valueUnitFlow);
}
use of eu.hansolo.tilesfx.chart.ChartData in project tilesfx by HanSolo.
the class SmoothedChartTileSkin method initGraphics.
// ******************** Initialization ************************************
@Override
protected void initGraphics() {
super.initGraphics();
chartEventEventHandler = e -> tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, new ChartData(e.getValue())));
titleText = new Text();
titleText.setFill(tile.getTitleColor());
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
xAxis = tile.getXAxis();
yAxis = tile.getYAxis();
chart = new SmoothedChart<>(xAxis, yAxis);
chart.setSmoothed(tile.isSmoothing());
chart.setAnimated(tile.isAnimated());
chart.setLegendSide(Side.TOP);
chart.setVerticalZeroLineVisible(false);
chart.setCreateSymbols(false);
chart.setSnapToTicks(tile.isSnapToTicks());
chart.setSymbolsVisible(tile.getDataPointsVisible());
switch(tile.getChartType()) {
case AREA:
chart.setChartType(SmoothedChart.ChartType.AREA);
break;
default:
chart.setChartType(SmoothedChart.ChartType.LINE);
break;
}
getPane().getChildren().addAll(titleText, chart);
// Add series not before chart is part of scene
chart.getData().setAll(tile.getTilesFXSeries().stream().map(tilesFxSeries -> tilesFxSeries.getSeries()).collect(Collectors.toList()));
// Adjust colors according to series settings
tile.getTilesFXSeries().stream().forEach(series -> chart.setSeriesColor(series.getSeries(), series.getStroke(), series.getFill(), series.getSymbolBackground(), series.getLegendSymbolFill()));
/*
Scene scene = chart.getScene();
if (scene != null) {
Window stage = scene.getWindow();
if (stage != null) {
if (stage.isShowing()) {
chart.getData().setAll(tile.getTilesFXSeries().stream().map(tilesFxSeries -> tilesFxSeries.getSeries()).collect(Collectors.toList()));
}
}
}
*/
}
Aggregations