AnyChart-Android icon indicating copy to clipboard operation
AnyChart-Android copied to clipboard

update line chart totally(title, column name etc..) with new values

Open JavaDeveloper08 opened this issue 5 years ago • 8 comments

Hi, I am using line chart and i would like to change line chart totally, not only data, for instance title, column name etc... I am changing these values by using parameters.

When i call this method, first data is shown in the chart, but the new chart is not shown, it stays the same chart. I tried your changing data section, I know but i don't want to change only data, I would like to change title, column name etc... May you help me please?

My code is like that ;

// I am calling this method from outside in activity, method parametrs "alarmItem" and "responseTelemetryDataList" are changing according the selection option.

loadChart(alarmItem, responseTelemetryDataList);

public void loadChart(Item item, List<ResponseTelemetryData> telemetryDataList) {

    AnyChartView anyChartView = findViewById(R.id.any_chart_view);

    anyChartView.setProgressBar(findViewById(R.id.progress_bar));

    Cartesian cartesian = AnyChart.line();

    cartesian.animation(true);

    cartesian.padding(10d, 20d, 5d, 20d);

    cartesian.crosshair().enabled(true);
    cartesian.crosshair()
            .yLabel(true)
            // TODO ystroke
            .yStroke((Stroke) null, null, null, (String) null, (String) null);

    cartesian.tooltip().positionMode(TooltipPositionMode.POINT);

    String departmentName = "";
    String deviceDescription = "";
    String alarmDescription = "";
    if (item != null) {
        if (item.getDepartment() != null)
            departmentName = item.getDepartment().getName();
        if (item.getDevice() != null)
            deviceDescription = item.getDevice().getDescription();
        if (item.getAlarmType() != null)
            alarmDescription = item.getAlarmType().getDescription();
    }
    StringBuilder title = new StringBuilder();

    if(!TextUtils.isEmpty(departmentName)) {
        title.append(departmentName);
        title.append(" ");
    }
    if(!TextUtils.isEmpty(deviceDescription)) {
        title.append(deviceDescription);
    }
    if(!TextUtils.isEmpty(departmentName)) {
        title.append(" (");
        title.append(alarmDescription);
        title.append(")");
    }

    cartesian.title(title.toString());

    String sensorName ="";
    if(item!=null) {
        if (item.getSensor() != null)
            if (item.getSensor().getName() != null)
                sensorName = item.getSensor().getName();
    }
    boolean isHumidity=false;
    boolean isTemperature=false;

    if(!TextUtils.isEmpty(sensorName)) {
        if(sensorName.equals("humidity")) {
            cartesian.yAxis(0).title(item.getSensor().getDescription() + " (%)");
            cartesian.yScale().ticks().interval(20);
            isHumidity=true;
        }
        else if(sensorName.equals("temperature")) {
            cartesian.yAxis(0).title(item.getSensor().getDescription() + " (" + item.getDeviceSensorRule().getDeviceSensor().getCommonUnits().getAbbr() + ")");
            cartesian.yScale().ticks().interval(10);
            isTemperature=true;
        }
        else {
            cartesian.yAxis(0).title(item.getSensor().getDescription());
            cartesian.yScale().ticks().interval(10);
        }
    }

    cartesian.yAxis(0).labels().padding(5d, 5d, 5d, 5d);

    cartesian.xAxis(0).labels().padding(3d, 3d, 3d, 3d);

    cartesian.yScale().minimum(0);


    if(sensorName.equals("humidity"))
        cartesian.yScale().ticks().interval(20);
    else
        cartesian.yScale().ticks().interval(10);

    List<DataEntry> seriesData = new ArrayList<>();

    int alarmValue=0;

    if(isHumidity || isTemperature) {
        if (item.getDeviceSensorRule() != null)
            alarmValue = Integer.valueOf(item.getDeviceSensorRule().getValue());
    }
    /*else
        alarmValue = Integer.valueOf(item.getGatewaySensorRule().getValue());*/

    if(telemetryDataList !=null) {

        for (int i = 0; i < telemetryDataList.size(); i++) {

            ResponseTelemetryData telemetryData = telemetryDataList.get(i);

            if (isHumidity || isTemperature)
                seriesData.add(new CustomDataEntry(SensrefUtil.convertStringFromDate(telemetryData.getTimestamp()), telemetryData.getValue(), alarmValue));
            else
                seriesData.add(new CustomDataEntry(SensrefUtil.convertStringFromDate(telemetryData.getTimestamp()), telemetryData.getValue()));

        
        }
    }

    set = Set.instantiate();
    set.data(seriesData);
    Mapping series1Mapping = set.mapAs("{ x: 'x', value: 'value' }");
    Mapping series2Mapping=null;
    if(alarmValue!=0)
        series2Mapping = set.mapAs("{ x: 'x', value: 'value2' }");
   Line series1 = cartesian.line(series1Mapping);

    series1.stroke("blue");
    series1.name("Sensör");
    series1.hovered().markers().enabled(true);
    series1.hovered().markers()
            .type(MarkerType.CIRCLE)
            .size(4d);
    series1.tooltip()
            .position("right")
            .anchor(Anchor.LEFT_CENTER)
            .offsetX(5d)
            .offsetY(5d);
    if(alarmValue!=0) {
        Line series2 = cartesian.line(series2Mapping);
        //series2. ({color: "yellow", dash: "2 2"});
        series2.stroke("red", 3, "3 3", " ", " ");
        series2.name("Alarm");
        series2.hovered().markers().enabled(true);
        series2.hovered().markers()
                .type(MarkerType.CIRCLE)
                .size(4d);

        series2.tooltip()
                .position("right")
                .anchor(Anchor.LEFT_CENTER)
                .offsetX(5d)
                .offsetY(5d);
    }

    cartesian.legend().enabled(true);
    cartesian.legend().fontSize(13d);
    cartesian.legend().padding(0d, 0d, 10d, 0d);

    anyChartView.setChart(cartesian);
}

JavaDeveloper08 avatar Nov 12 '19 11:11 JavaDeveloper08

@JavaDeveloper08 You should make the chart active and apply the new data just we showed in previous issues and comments (you have already seen them). And along with the new data you can update the chart settings. To update any chart setting - simply apply new value via API. For example, to change the chart title - cartesian .title("New title"). The same approach is available for any other setting via API.

Shestac92 avatar Nov 13 '19 02:11 Shestac92

Thanks for reply, I updated the data and other information, but I have a problem in this point. The hover line that selects a point in the chart stays from previous chart on the screen. If I choose a new point in the chart, yes new point is shown. My update code is like that ;

                  APIlib.getInstance().setActiveAnyChartView(anyChartView);
                  cartesian.removeAllSeries();
                  
                    cartesian.title(getTitle(alarmItem));
                    cartesian.yAxis(0).title(alarmItem.getSensor().getDescription() + " (%)");
                    cartesian.yScale().ticks().interval(20);
                    cartesian.animation(true);

                    sensorName = getSensorName(alarmItem);

                    if(!TextUtils.isEmpty(sensorName)) {
                        if(sensorName.equals("humidity")) {
                            humidityPercentMark = " % ";
                            cartesian.yAxis(0).title(alarmItem.getSensor().getDescription() + " (%)");
                            cartesian.yScale().ticks().interval(20);
                            isHumidity=true;
                        }
                        else if(sensorName.equals("temperature")) {
                            celciusMark = " "+alarmItem.getDeviceSensorRule().getDeviceSensor().getCommonUnits().getAbbr();
                            cartesian.yAxis(0).title(alarmItem.getSensor().getDescription() + " (" + celciusMark + ")");
                            cartesian.yScale().ticks().interval(10);
                            isTemperature=true;
                        }
                        else {
                            cartesian.yAxis(0).title(alarmItem.getSensor().getDescription());
                            cartesian.yScale().ticks().interval(10);
                        }
                    }

                    cartesian.yAxis(0).labels().padding(5d, 5d, 5d, 5d);

                    cartesian.xAxis(0).labels().padding(3d, 3d, 3d, 3d);

                    cartesian.yScale().minimum(0);

                    if(sensorName.equals("humidity"))
                        cartesian.yScale().ticks().interval(20);
                    else
                        cartesian.yScale().ticks().interval(10);

                    int alarmValue=0;

                    if(isHumidity || isTemperature) {
                        cartesian.interactivity().hoverMode(HoverMode.BY_X);
                        if(isHumidity)
                            cartesian.tooltip()
                                    .valuePrefix(humidityPercentMark)
                                    .valuePostfix(" ")
                                    .displayMode(TooltipDisplayMode.UNION);
                        else if(isTemperature)
                            cartesian.tooltip()
                                    .valuePrefix(" ")
                                    .valuePostfix(celciusMark)
                                    .displayMode(TooltipDisplayMode.UNION);

                        if (alarmItem.getDeviceSensorRule() != null)
                            alarmValue = Integer.valueOf(alarmItem.getDeviceSensorRule().getValue());
                    }


                    Mapping series1Mapping = set.mapAs("{ x: 'x', value: 'value' }");
                    Mapping series2Mapping=null;
                    if(alarmValue!=0)
                        series2Mapping = set.mapAs("{ x: 'x', value: 'value2' }");

                    Line series1 = cartesian.line(series1Mapping);

                    series1.stroke("blue");
                    series1.name("Sensör");
                    series1.hovered().markers().enabled(true);
                    series1.hovered().markers()
                            .type(MarkerType.CIRCLE)
                            .size(4d);
                    series1.tooltip()
                            .position("right")
                            .anchor(Anchor.LEFT_CENTER)
                            .offsetX(5d)
                            .offsetY(5d);
                    if(!TextUtils.isEmpty(sensorName)) {
                        if(!sensorName.equals("heartbeat") && alarmValue!=0) {
                            Line series2 = cartesian.line(series2Mapping);
                            series2.stroke("red", 3, "3 3", " ", " ");
                            series2.name("Alarm");
                            series2.hovered().markers().enabled(true);
                            series2.hovered().markers()
                                    .type(MarkerType.CIRCLE)
                                    .size(4d);

                            series2.tooltip()
                                    .position("right")
                                    .anchor(Anchor.LEFT_CENTER)
                                    .offsetX(5d)
                                    .offsetY(5d);
                        }
                    }
                    set.data(getSeriesData(responseTelemetryDataList));

May you help me ?

JavaDeveloper08 avatar Nov 14 '19 09:11 JavaDeveloper08

@JavaDeveloper08 The hovering line is placed according to the hovered point index. Even if that point was totally changed, the hover line stays in the same position. Unfortunately, in the current version of the library, there's no possibility to override this behavior.

Shestac92 avatar Nov 14 '19 10:11 Shestac92

Ok, I examined your answer in https://github.com/AnyChart/AnyChart-Android/issues/97 I am not writing "anyChartView.setChart(cartesian);" again. Only I updated the values.

I have one layout in activity. I am studying only line chart. So, I dynamically update line chart according to selection of user.

May I use "anyChartView.clear()" and "anyChartView.setChart(cartesian);" after setting data.

I tried, chart is refreshing but, same data stays and not changing.

Thanks for your help.

JavaDeveloper08 avatar Nov 14 '19 11:11 JavaDeveloper08

@JavaDeveloper08 Yes, you can clear the layout, but then you should create the chart again and apply new data to it.

Shestac92 avatar Nov 15 '19 03:11 Shestac92

I have same problem

VinayPatel1997 avatar Oct 03 '20 15:10 VinayPatel1997

If you found solution then please let me know

VinayPatel1997 avatar Oct 03 '20 15:10 VinayPatel1997

@JavaDeveloper08 Yes, you can clear the layout, but then you should create the chart again and apply new data to it.

I'm unable to clear layout. when I clear layout chart disappear and didn't create with new data

VinayPatel1997 avatar Oct 03 '20 16:10 VinayPatel1997