Search code examples
mpandroidchartios-charts

Make line chart with values and dates


In my app i use ios-charts library (swift alternative of MPAndroidChart). All i need is to display line chart with dates and values.

Right now i use this function to display chart

func setChart(dataPoints: [String], values: [Double]) {       

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }        

    let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Items count")
    let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
    dateChartView.data = lineChartData      
}

And this is my data:

xItems = ["27.05", "03.06", "17.07", "19.09", "20.09"] //String    
let unitsSold = [25.0, 30.0, 45.0, 60.0, 20.0] //Double

But as you can see - xItems are dates in "dd.mm" format. As they are strings they have same paddings between each other. I want them to be more accurate with real dates. For example 19.09 and 20.09 should be very close. I know that i should match each day with some number in order to accomplish it. But i don't know what to do next - how i can adjust x labels margins?

UPDATE After small research where i found out that many developers had asked about this feature but nothing happened - for my case i found very interesting alternative to this library in Swift - PNChart. It is easy to use, it solves my problem.


Solution

  • The easiest solution will be to loop through your data and add a ChartDataEntry with a value of 0 and a corresponding label for each missing date.

    In response to the question in the comments here is a screenshot from one of my applications where I am filling in date gaps with 0 values:

    Chart Gaps

    In my case I wanted the 0 values rather than an averaged line from data point to data point as it clearly indicates there is no data on the days skipped (8/11 for instance).

    From @Philipp Jahoda's comments it sounds like you could skip the 0 value entries and just index the data you have to the correct labels.

    I modified the MPAndroidChart example program to skip a few data points and this is the result:

    Chart Gaps Non Zero

    As @Philipp Jahoda mentioned in the comments the chart handles missing Entry by just connecting to the next data point. From the code below you can see that I am generating x values (labels) for the entire data set but skipping y values (data points) for index 11 - 29 which is what you want. The only thing remaining would be to handle the x labels as it sounds like you don't want 15, 20, and 25 in my example to show up.

    ArrayList<String> xVals = new ArrayList<String>();
    for (int i = 0; i < count; i++) {
        xVals.add((i) + "");
    }
    
    ArrayList<Entry> yVals = new ArrayList<Entry>();
    
    for (int i = 0; i < count; i++) {
    
        if (i > 10 && i < 30) {
            continue;
        }
    
        float mult = (range + 1);
        float val = (float) (Math.random() * mult) + 3;// + (float)
        // ((mult *
        // 0.1) / 10);
        yVals.add(new Entry(val, i));
    }