hellocharts-android icon indicating copy to clipboard operation
hellocharts-android copied to clipboard

Set Label to the individual point

Open quki opened this issue 11 years ago • 2 comments

Hello, Firstly, Thank you for releasing such an awesome Chart Library, HelloCharts. I have one question for you.
I would like to set the Label at individual point, not every point and not selected one. Below codes are that i used to set label at all the points and at selected one.

line.setHasLabels(true);
line.setHasLabelsOnlyForSelected(true);

But I want to sync progressbar time(audio stream time) with graph X value so that set a individual label matched by progressbar time.

Like this below picture. But this picture shows selected mode, I don't want to use click or touch event , Just i want Label to be moved one by one depends on progressbar time.

picture

I saw class files(Line.class, AbstractChartView.class...) in .jar, But i was unable to find the answer.
Please let me know what should I do. Thank you for reading !. Have a nice day.

quki avatar Aug 24 '15 08:08 quki

Hi, it will be difficult do that with just single line. I think you can used two lines. First line will display all points. Second line will display only one point corresponding to current state of progress bar. So for the second line you will be changing point X value when user change progress bar state. You can set slightly larger point radius and a little darker color for the second line to highlight current state.

//in onCreateView() ...
//Prepare data
List<Line> lines = new ArrayList<Line>();
for (int i = 0; i < 2; ++i) {//two lines, second one has to have only one point
    List<PointValue> values = new ArrayList<PointValue>();
    for (int j = 0; j < 10; ++j) {//10 points for example but only for first line
        if(i == 1 && j == 0) {
            //second line, first point, break because we want to add only one point to the second line, with the same Y value as the first point on the first line
            values.add(new PointValue(j, lines.get(0).getValues().get(0).getY()));
            break;
        } else {
            values.add(new PointValue(j, (float) Math.random() * 100f));//adding point to the first line
        }
    }

Line line = new Line(values);
line.setColor(Color.BLUE);
    if(i == 1) {
        //set labels enabled for second line
        line.setHasLabels(true);
    } else {
        line.setHasLabels(false);
    }
        lines.add(line);
}
data = new LineChartData(lines);
chart.setLineChartData(data);

//Prepare seek bar listener
SeekBar bar = (SeekBar) rootView.findViewById(R.id.progressbar);
bar.incrementProgressBy(1);
bar.setMax(10);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        //For safety create copy of the chart's data
        LineChartData data = new LineChartData(chart.getLineChartData());
        //get Y value for point on the first line at index == progress - 1(because indexed from 0 to 9)
        float line0ValueY = data.getLines().get(0).getValues().get(progress - 1).getY();
        //update single point on the second line
        data.getLines().get(1).getValues().get(0).set(progress - 1, line0ValueY);
        //replace chart data
        chart.setLineChartData(data);
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
});

device-2015-08-24-194312

lecho avatar Aug 24 '15 17:08 lecho

What a beautiful advice for this issue. I will adjust your instruction. Thank you for your detailed helping !

quki avatar Aug 25 '15 14:08 quki