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

Time series support?

Open Lir10 opened this issue 9 years ago • 19 comments

Hello ,

I wanted to start using this library but i dont see any support for time series chart?

I want to make time series in the X axis and custom Y labels. is it possible? Thanks.

Lir10 avatar May 18 '15 07:05 Lir10

I am not 100% sure if this is the same issue... I am facing the problem that I want to put multiple time series data as lines into one chart. Now my data has different time ranges such as:

  1. First data set: Tuesday to Thursday
  2. Second data set: Monday to Friday

I noticed that I can add the timestamp as a label - which is nothing more as a string which aligns with the x-value in the data line (not shown in the screenshot). When I add the data lines to the chart they both start at the far left as shown in the screenshot.

hellochart

Is the line chart actually aware of the date data type? At the moment all data points have the same horizontal distance from each other - which is simply wrong.

johnjohndoe avatar May 19 '15 13:05 johnjohndoe

Thanks for the answer looks like it's something similar but still i just want to create a graph with time series on x axis. any ideas?

Lir10 avatar May 19 '15 14:05 Lir10

Currently, I put the timestamp from each data item onto the x-axis. Here is how:

List<PointValue> yValues = new ArrayList<PointValue>();
List<AxisValue> axisValues = new ArrayList<AxisValue>();
for (int x = 0; x < mData.size(); ++x) {
    DataItem dataItem = mData.getItem(x);
    DateTime recordedAt = dataItem.getRecordedAt();
    String formattedRecordedAt = DateTimeFormatter.getFormattedDateTime(recordedAt);
    int yValue = dataItem.getYValue();
    yValues.add(new PointValue(x, yValue));
    AxisValue axisValue = new AxisValue(x);
    axisValue.setLabel(formattedRecordedAt);
    axisValues.add(axisValue);
}

// then later ...

LineChartData lineChartData = new LineChartData();
lineChartData.setLines(mLines);
lineChartData.setAxisXBottom(new Axis(axisValues));

As mentioned it is nothing but a string label. All data points have the same distance from each other which is not what the timestamps aka. x-values say.

johnjohndoe avatar May 19 '15 14:05 johnjohndoe

Thanks i will try that !

Lir10 avatar May 19 '15 14:05 Lir10

Issue #12 of MPAndroidChart describes the same problem - they do not support time series by now.

johnjohndoe avatar May 19 '15 14:05 johnjohndoe

yeah its pretty bad.. this library would be perfect if time series was supported

Lir10 avatar May 19 '15 14:05 Lir10

@lecho Please notice that @kenaiX mentioned here that AChartEngine supports time series ... This might be a source for you.

johnjohndoe avatar May 19 '15 16:05 johnjohndoe

yes i already used AChartEngine but the problem that it's very slow for big data set and you may not be able to zoom/scroll

Lir10 avatar May 19 '15 16:05 Lir10

Hi, yea, hellocharts doesn't have dedicated methods for time series. Instead for now you need to use string labels. I will look into AChartEngine implementation to see how they did that.

@johnjohndoe I think you could use timestamps as X values to display points with different X distances from each other.

List<PointValue> yValues = new ArrayList<PointValue>();
List<AxisValue> axisValues = new ArrayList<AxisValue>();
for (int i = 0; i < mData.size(); ++i) {
    DataItem dataItem = mData.getItem(i);
    DateTime recordedAt = dataItem.getRecordedAt();
    String formattedRecordedAt = DateTimeFormatter.getFormattedDateTime(recordedAt);
    int yValue = dataItem.getYValue();
    yValues.add(new PointValue(recordedAt.toMiliseconds(), yValue)); //use recordedAt timestamp/day of the year number or something like that as x value
    AxisValue axisValue = new AxisValue(recordedAt.toMiliseconds());
    axisValue.setLabel(formattedRecordedAt);
    axisValues.add(axisValue);
}

// then later ...

LineChartData lineChartData = new LineChartData();
lineChartData.setLines(mLines);
lineChartData.setAxisXBottom(new Axis(axisValues));

lecho avatar May 19 '15 18:05 lecho

@lecho Thank you. This seems to be a good workaround.

johnjohndoe avatar May 20 '15 15:05 johnjohndoe

@lecho Is there an ETA for time series support?

hfahmy avatar Jul 14 '15 06:07 hfahmy

Hi, no, I am not going to add time series any time soon

lecho avatar Jul 14 '15 18:07 lecho

Honestly, believe it or not, this works pretty reliably for me, and faster than any other library I've tried

  1. Decrement all the X in PointValue ArrayList by 1
  2. Add the new point in the PointValue ArrayList
  3. Call setLineChartData for the chartView.

If you want to "scroll", remove the PointValues that have the new X value less than your requirement.

I haven't played with viewport for timeseries data but maybe I will later and then the timeseries data will be "pannable".

AfzalivE avatar Jan 05 '16 02:01 AfzalivE

@lecho I am using the following code to implement the time stamp fro the plot

> List<PointValue> yValues = new ArrayList<PointValue>();
> List<AxisValue> axisValues = new ArrayList<AxisValue>();
> for (int i = 0; i < mData.size(); ++i) {
>     DataItem dataItem = mData.getItem(i);
>     DateTime recordedAt = dataItem.getRecordedAt();
>     String formattedRecordedAt = DateTimeFormatter.getFormattedDateTime(recordedAt);
>     int yValue = dataItem.getYValue();
>     yValues.add(new PointValue(recordedAt.toMiliseconds(), yValue)); //use recordedAt  timestamp/day of the year number or something like that as x value
>     AxisValue axisValue = new AxisValue(recordedAt.toMiliseconds());
>     axisValue.setLabel(formattedRecordedAt);
>     axisValues.add(axisValue);
> }
> 
> // then later ...
> 
> LineChartData lineChartData = new LineChartData();
> lineChartData.setLines(mLines);
> lineChartData.setAxisXBottom(new Axis(axisValues));

Its the same code @johnjohndoe suggested. I'm quite new to this, I did not understand how DataItem and DateTimeFormatter is being retrieved. It would really helpful if you could provide with some information regarding the same.

adarsh-tm avatar Jun 02 '16 08:06 adarsh-tm

I have the same issue. How do we implement time series and is its support added now? @

smsubham avatar Aug 07 '16 07:08 smsubham

@adarsh-tm Please note that you can wrap code snippets in triple backticks ``` to enable nice syntax highlighting. The quote signs > can be omitted then.

johnjohndoe avatar Aug 07 '16 11:08 johnjohndoe

I have added the real time graph with time in x axis for Line chart. The drawback for this is that the interval of adding data has to be uniform Create a variable which holds the current time. This should be initialized when you add the first entry to the Line data graphStartTime = System.currentTimeMillis(); Now use the stored value in the valueformatter

xAxis.setValueFormatter(new IAxisValueFormatter() { private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()); @Override public String getFormattedValue(float value, AxisBase axis) { long currentTime = graphStartTime + ((int)value * intervalAtWhichYouUpdateTheLineData);

            return dateFormat.format(new Date(currentTime));
        }
    });

xAxis.setLabelCount(maxXAxisLabelCount); // Add a limit otherwise the labels overlap

vijvin avatar Jun 28 '17 12:06 vijvin

Can you please post a small demo code for multi line graphs as shown in the above image.?

rishabhtech1994 avatar Apr 04 '19 09:04 rishabhtech1994

@rishabhtech1994 A dataset entry can hold a custom object, I created a class to hold my time data with some other information and created the entry set. when overriding getFormattedValue for xAxis I can retrieve the data object from the entry. There own you have to just format the time as you need it and return the string

vijvin avatar Apr 19 '19 09:04 vijvin