AnyChart / AnyChart-Android

AnyChart Android Chart is an amazing data visualization library for easily creating interactive charts in Android apps. It runs on API 19+ (Android 4.4) and features dozens of built-in chart types.
2.31k stars 369 forks source link

How to give more than one data set in Choropleth map? #58

Open aadesh0118 opened 5 years ago

aadesh0118 commented 5 years ago

I am showing diesel prices in each state. When the user clicks on petrol prices now the map should show petrol prices in each state? How do i do that?

Shestac92 commented 5 years ago

@aadesh0118 Do you mean that initially, the user sees prices for every state on a country map? Then the user clicks on a required state and the map drill-down to this state and shows prices in every region of this state. Did I get your idea correctly?

aadesh0118 commented 5 years ago

No, Initially the user sees prices for diesel for every state on a country map. The there is a button that the user clicks and now the every state will have petrol prices on the country map.

Shestac92 commented 5 years ago

@aadesh0118 Here is the code of the map you requested

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

        final Map map = AnyChart.map();

        final List<DataEntry> dieselData = new ArrayList<>();
        dieselData.add(new CustomDataEntry("US.MA", 2.422));
        dieselData.add(new CustomDataEntry("US.MN", 2.518));
        dieselData.add(new CustomDataEntry("US.MT", 2.365));
        dieselData.add(new CustomDataEntry("US.ND", 2.415));
        dieselData.add(new CustomDataEntry("US.HI", 2.498));
        dieselData.add(new CustomDataEntry("US.ID", 2.504));
        dieselData.add(new CustomDataEntry("US.WA", 2.415));

        final List<DataEntry> petrolData = new ArrayList<>();
        petrolData.add(new CustomDataEntry("US.MA", 2.95));
        petrolData.add(new CustomDataEntry("US.MN", 3.13));
        petrolData.add(new CustomDataEntry("US.MT", 3.05));
        petrolData.add(new CustomDataEntry("US.ND", 3.24));
        petrolData.add(new CustomDataEntry("US.HI", 3.07));
        petrolData.add(new CustomDataEntry("US.ID", 3.21));
        petrolData.add(new CustomDataEntry("US.WA", 3.14));

        Choropleth seriesDiesel = map.choropleth(dieselData);
        LinearColor linearColor = LinearColor.instantiate();
        linearColor.colors(new String[]{"#deebf7", "#3182bd"});
        seriesDiesel.colorScale(linearColor);
        seriesDiesel.hovered().fill("#addd8e");

        map.geoData("anychart.maps.united_states_of_america");

        final Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                map.removeAllSeries();
                if (button.getText().toString() == "Petrol") {
                    Choropleth seriesPetrol = map.choropleth(petrolData);
                    LinearColor linearColor = LinearColor.instantiate();
                    linearColor.colors(new String[]{"#deebf7", "#3182bd"});
                    seriesPetrol.colorScale(linearColor);
                    seriesPetrol.hovered().fill("#addd8e");
                    button.setText("Diesel");
                } else {
                    Choropleth seriesDiesel = map.choropleth(dieselData);
                    LinearColor linearColor = LinearColor.instantiate();
                    linearColor.colors(new String[]{"#deebf7", "#3182bd"});
                    seriesDiesel.colorScale(linearColor);
                    seriesDiesel.hovered().fill("#addd8e");
                    button.setText("Petrol");
                }
            }
        });

        anyChartView.addScript("file:///android_asset/united_states_of_america.js");
        anyChartView.addScript("file:///android_asset/proj4.js");
        anyChartView.setChart(map);
}

class CustomDataEntry extends DataEntry {
        public CustomDataEntry(String id, Number value) {
            setValue("id", id);
            setValue("value", value);
        }
}