ChartSelectionContext

2 posts, 0 answers
  1. Anandhi
    Anandhi avatar
    1 posts
    Member since:
    May 2016

    Posted 31 May Link to this post

    HI,

    I have a RadCartesianChartView with two line series(LineSeries1,LineSeries2) . I am adding  the line series one by one.The series overlap with each other as shown in the screen shot. The problem is that when I select the  LineSeries1,it shows all the datapoints in LineSeries2.I have attached the the screenshot.

    Thanks.

     

    package itd.co.za.teleriksamples;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.ViewGroup;

    import com.telerik.widget.chart.engine.databinding.DataPointBinding;
    import com.telerik.widget.chart.engine.databinding.FieldNameDataPointBinding;
    import com.telerik.widget.chart.visualization.behaviors.ChartPanAndZoomBehavior;
    import com.telerik.widget.chart.visualization.behaviors.ChartSelectionBehavior;
    import com.telerik.widget.chart.visualization.behaviors.ChartSelectionChangeListener;
    import com.telerik.widget.chart.visualization.behaviors.ChartSelectionContext;
    import com.telerik.widget.chart.visualization.behaviors.ChartSelectionMode;
    import com.telerik.widget.chart.visualization.behaviors.ChartTooltipBehavior;
    import com.telerik.widget.chart.visualization.behaviors.ChartTrackBallBehavior;
    import com.telerik.widget.chart.visualization.behaviors.ChartZoomStrategy;
    import com.telerik.widget.chart.visualization.cartesianChart.CartesianChartGrid;
    import com.telerik.widget.chart.visualization.cartesianChart.GridLineVisibility;
    import com.telerik.widget.chart.visualization.cartesianChart.RadCartesianChartView;
    import com.telerik.widget.chart.visualization.cartesianChart.axes.CategoricalAxis;
    import com.telerik.widget.chart.visualization.cartesianChart.axes.LinearAxis;
    import com.telerik.widget.chart.visualization.cartesianChart.series.categorical.LineSeries;
    import com.telerik.widget.chart.visualization.cartesianChart.series.categorical.SphericalDataPointIndicatorRenderer;
    import com.telerik.widget.chart.visualization.common.ChartSeries;

    import java.util.ArrayList;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {
    private List<MonthResult> lineValues;
    private List<MonthResult> splineValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    RadCartesianChartView chartView = new RadCartesianChartView(this);
    ViewGroup rootView = (ViewGroup) findViewById(R.id.container1);

    rootView.addView(chartView);
    initData();
    createLineSeriesGraph(chartView, rootView);

    //createSplineSeriesGraph(chartView);


    }


    /**
    * Method to create the line graph
    */
    private void createLineSeriesGraph(RadCartesianChartView chartView, ViewGroup rootView) {

    LinearAxis verticalAxis = new LinearAxis();
    CategoricalAxis horizontalAxis = new CategoricalAxis();

    DataPointBinding monthBinding = new FieldNameDataPointBinding("month");
    DataPointBinding resultBinding = new FieldNameDataPointBinding("result");

    LineSeries lineSeries = new LineSeries();
    lineSeries.setStrokeThickness(2F);
    lineSeries.setLegendTitle("Sales");
    lineSeries.setCategoryBinding(monthBinding);
    lineSeries.setValueBinding(resultBinding);
    lineSeries.setData(this.lineValues);

    SphericalDataPointIndicatorRenderer renderer = new SphericalDataPointIndicatorRenderer(lineSeries);
    lineSeries.setDataPointIndicatorRenderer(renderer);

    LineSeries splineSeries = new LineSeries();
    splineSeries.setStrokeThickness(2F);
    splineSeries.setLegendTitle("3Mth Avg");

    splineSeries.setCategoryBinding(monthBinding);
    splineSeries.setValueBinding(resultBinding);
    splineSeries.setData(this.splineValues);

    // Add Spherical points in the graph
    // renderer.setPointIndicatorColor(Color.GREEN);
    splineSeries.setDataPointIndicatorRenderer(new SphericalDataPointIndicatorRenderer(splineSeries));


    CartesianChartGrid grid = new CartesianChartGrid();
    grid.setStripLinesVisibility(GridLineVisibility.Y);
    chartView.setGrid(grid);

    chartView.setHorizontalAxis(horizontalAxis);


    chartView.setVerticalAxis(verticalAxis);

    prepareChart(chartView);
    // splineSeries.setShowLabels(true);
    chartView.getSeries().add(lineSeries);
    chartView.getSeries().add(splineSeries);


    ChartTooltipBehavior tooltipBehavior = new ChartTooltipBehavior(this);
    chartView.getBehaviors().add(tooltipBehavior);


    ChartTrackBallBehavior trackballBehavior = new ChartTrackBallBehavior(this);
    chartView.getBehaviors().add(trackballBehavior);

    ChartPanAndZoomBehavior behavior = new ChartPanAndZoomBehavior();
    chartView.getBehaviors().add(behavior);
    behavior.setZoomStrategy(ChartZoomStrategy.DEFERRED);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }

    return super.onOptionsItemSelected(item);
    }

    /**
    * Covenience method to add data
    */
    private void initData() {
    lineValues = new ArrayList<MonthResult>();
    lineValues.add(new MonthResult("Jan", 10));
    lineValues.add(new MonthResult("Feb", 5));
    lineValues.add(new MonthResult("Mar", 45));
    lineValues.add(new MonthResult("Apr", 10));
    lineValues.add(new MonthResult("May", 43));

    splineValues = new ArrayList<MonthResult>();
    splineValues.add(new MonthResult("Jan", 40));
    splineValues.add(new MonthResult("Feb", 15));
    splineValues.add(new MonthResult("Mar", 25));
    splineValues.add(new MonthResult("Apr", 07));
    splineValues.add(new MonthResult("May", 45));
    }

    /**
    * Method to set change listener for the chart
    * @param chartView
    */
    protected void prepareChart(RadCartesianChartView chartView) {
    ChartSelectionBehavior selectionBehavior = new ChartSelectionBehavior();
    chartView.getBehaviors().add(selectionBehavior);
    selectionBehavior.setSelectionChangeListener(new ChartSelectionChangeListener() {
    @Override
    public void onSelectionChanged(ChartSelectionContext chartSelectionContext) {
    ChartSeries selectedSeries = chartSelectionContext.selectedSeries();
    if (selectedSeries != null) {
    if (canSelectSeries(selectedSeries)) {
    selectedSeries.setShowLabels(true);
    }
    }

    ChartSeries deselectedSeries = chartSelectionContext.deselectedSeries();
    if (deselectedSeries != null) {
    deselectedSeries.setShowLabels(false);
    }
    }
    });

    this.initSelectionBehavior(selectionBehavior);


    }



    protected boolean canSelectSeries(ChartSeries series){
    return true;
    }

    /**
    * Method to initialse the selection behaviour
    * @param behavior
    */
    protected void initSelectionBehavior(ChartSelectionBehavior behavior) {
    behavior.setDataPointsSelectionMode(ChartSelectionMode.MULTIPLE);
    behavior.setSeriesSelectionMode(ChartSelectionMode.MULTIPLE);
    }
    }
  2. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 31 May Link to this post

    Hello Anandhi,

    Thanks for writing.
    There is a bug in the current release related to the line and area selection. It has been fixed in the development builds but they are not released yet. The fix will be available in our next official release.
    Please write again if you have more feedback to share.

    Regards,
    Victor
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
Back to Top