Search in sources :

Example 1 with LineChartRowData

use of org.eclipse.rap.chartjs.line.LineChartRowData in project rap by entirej.

the class EJRWTLineChartRecordBlockRenderer method refresh.

public void refresh(Object input) {
    if (_chartView != null && !_chartView.isDisposed()) {
        if (xAxisColumn == null && xAxisColumn.isEmpty()) {
            return;
        }
        Map<Object, Map<String, Float>> dataset = new HashMap<Object, Map<String, Float>>();
        Map<Object, Map<String, EJCoreVisualAttributeProperties>> datasetVa = new HashMap<Object, Map<String, EJCoreVisualAttributeProperties>>();
        Collection<EJDataRecord> records = _block.getRecords();
        Collection<EJScreenItemController> screenItems = getScreenItems();
        List<Object> labelsIndex = new ArrayList<Object>();
        Map<String, Number> lastVal = new HashMap<String, Number>();
        for (EJDataRecord ejDataRecord : records) {
            Object xobject = ejDataRecord.getValue(xAxisColumn);
            if (xobject != null) {
                Map<String, Float> set = dataset.get(xobject);
                Map<String, EJCoreVisualAttributeProperties> setVa = datasetVa.get(xobject);
                if (set == null) {
                    set = new HashMap<String, Float>();
                    setVa = new HashMap<String, EJCoreVisualAttributeProperties>();
                    dataset.put(xobject, set);
                    datasetVa.put(xobject, setVa);
                    labelsIndex.add(xobject);
                }
                for (EJScreenItemController sItem : screenItems) {
                    if (!sItem.isSpacerItem()) {
                        Object yvalue = ejDataRecord.getValue(sItem.getName());
                        Float val = null;
                        if (yvalue instanceof Number) {
                            lastVal.put(sItem.getName(), (Number) yvalue);
                            val = ((Number) yvalue).floatValue();
                        } else {
                            Number last = lastVal.get(sItem.getName());
                            val = last != null ? last.floatValue() : 0f;
                        }
                        set.put(sItem.getName(), val);
                        EJCoreVisualAttributeProperties visualAttribute = ejDataRecord.getItem(sItem.getName()).getVisualAttribute();
                        if (visualAttribute == null)
                            visualAttribute = sItem.getItemRenderer().getVisualAttributeProperties();
                        setVa.put(sItem.getName(), visualAttribute);
                    }
                }
            }
        }
        List<String> xlabel = new ArrayList<String>(labelsIndex.size());
        for (Object object : labelsIndex) {
            String xvalue;
            xvalue = getStrValue(object);
            xlabel.add(xvalue);
        }
        LineChartRowData chartRowData = new LineChartRowData(xlabel.toArray(new String[0]));
        for (EJScreenItemController sItem : screenItems) {
            if (sItem.isSpacerItem())
                continue;
            List<Float> row = new ArrayList<Float>();
            List<EJCoreVisualAttributeProperties> rowVa = new ArrayList<EJCoreVisualAttributeProperties>();
            for (Object object : labelsIndex) {
                Map<String, Float> map = dataset.get(object);
                Map<String, EJCoreVisualAttributeProperties> mapVa = datasetVa.get(object);
                if (map == null || mapVa == null)
                    continue;
                row.add(map.get(sItem.getName()));
                rowVa.add(mapVa.get(sItem.getName()));
            }
            float[] floatArray = new float[row.size()];
            ChartStyle[] styleArray = new ChartStyle[row.size()];
            int i = 0;
            for (Float f : row) {
                floatArray[i] = (f != null ? f : 0);
                ChartStyle colors = new ChartStyle(220, 220, 220, 0.8f);
                EJCoreVisualAttributeProperties attributeProperties = rowVa.get(i);
                if (attributeProperties != null) {
                    if (attributeProperties.getForegroundColor() != null) {
                        Color color = attributeProperties.getForegroundColor();
                        colors = new ChartStyle(color.getRed(), color.getGreen(), color.getBlue(), 0.8f);
                    }
                    if (attributeProperties.getBackgroundColor() != null) {
                        Color color = attributeProperties.getBackgroundColor();
                        colors.setFillColor(new RGB(color.getRed(), color.getGreen(), color.getBlue()));
                    }
                }
                styleArray[i] = colors;
                i++;
            }
            /*
                 * String pointStyle = "circle";
                 * 
                 * ChartStyle chartStyle;
                 */
            LineChartRowData.RowInfo info = new LineChartRowData.RowInfo();
            String label = sItem.getProperties().getLabel();
            if (label.trim().isEmpty()) {
                label = sItem.getProperties().getReferencedItemName();
            }
            ChartStyle colors = new ChartStyle(220, 220, 220, 0.8f);
            EJCoreVisualAttributeProperties attributeProperties = sItem.getItemRenderer().getVisualAttributeProperties();
            if (attributeProperties != null) {
                if (attributeProperties.getForegroundColor() != null) {
                    Color color = attributeProperties.getForegroundColor();
                    colors = new ChartStyle(color.getRed(), color.getGreen(), color.getBlue(), 0.8f);
                }
                if (attributeProperties.getBackgroundColor() != null) {
                    Color color = attributeProperties.getBackgroundColor();
                    colors.setFillColor(new RGB(color.getRed(), color.getGreen(), color.getBlue()));
                }
            }
            info.setLabel(label);
            info.setChartStyle(colors);
            info.setHidden(!sItem.isVisible());
            EJCoreMainScreenItemProperties mainScreenItemProperties = (EJCoreMainScreenItemProperties) sItem.getProperties();
            info.setFill(mainScreenItemProperties.getBlockRendererRequiredProperties().getBooleanProperty(SHOW_FILL, info.isFill()));
            String action = mainScreenItemProperties.getBlockRendererRequiredProperties().getStringProperty("action");
            if (action == null || action.trim().isEmpty()) {
                action = "select";
            }
            info.setAction(action);
            info.setPointStyle(mainScreenItemProperties.getBlockRendererRequiredProperties().getStringProperty(POINT_STYLE));
            info.setShowLine(mainScreenItemProperties.getBlockRendererRequiredProperties().getBooleanProperty(SHOW_LINE, info.isShowLine()));
            info.setLineWidth(mainScreenItemProperties.getBlockRendererRequiredProperties().getIntProperty(LINE_WIDTH, info.getLineWidth()));
            info.setLineTension(mainScreenItemProperties.getBlockRendererRequiredProperties().getFloatProperty(LINE_TENSION, (float) info.getLineTension()));
            info.setSteppedLine(mainScreenItemProperties.getBlockRendererRequiredProperties().getStringProperty(STEPPED_LINE));
            chartRowData.addRow(info, floatArray, styleArray);
        // chartRowData.addRow(floatArray, colors);
        }
        _chartView.load(chartRowData, options);
    }
}
Also used : HashMap(java.util.HashMap) LineChartRowData(org.eclipse.rap.chartjs.line.LineChartRowData) ArrayList(java.util.ArrayList) EJCoreMainScreenItemProperties(org.entirej.framework.core.properties.EJCoreMainScreenItemProperties) ChartStyle(org.eclipse.rap.chartjs.ChartStyle) Color(java.awt.Color) RGB(org.eclipse.swt.graphics.RGB) EJScreenItemController(org.entirej.framework.core.interfaces.EJScreenItemController) JsonObject(org.eclipse.rap.json.JsonObject) Map(java.util.Map) HashMap(java.util.HashMap) EJDataRecord(org.entirej.framework.core.data.EJDataRecord) EJCoreVisualAttributeProperties(org.entirej.framework.core.properties.EJCoreVisualAttributeProperties)

Aggregations

Color (java.awt.Color)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ChartStyle (org.eclipse.rap.chartjs.ChartStyle)1 LineChartRowData (org.eclipse.rap.chartjs.line.LineChartRowData)1 JsonObject (org.eclipse.rap.json.JsonObject)1 RGB (org.eclipse.swt.graphics.RGB)1 EJDataRecord (org.entirej.framework.core.data.EJDataRecord)1 EJScreenItemController (org.entirej.framework.core.interfaces.EJScreenItemController)1 EJCoreMainScreenItemProperties (org.entirej.framework.core.properties.EJCoreMainScreenItemProperties)1 EJCoreVisualAttributeProperties (org.entirej.framework.core.properties.EJCoreVisualAttributeProperties)1