使用值和日期制作折线图 [英] Make line chart with values and dates

查看:256
本文介绍了使用值和日期制作折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用ios-charts库(MPAndroidChart的快速替代品). 我需要的是显示带有日期和值的折线图.

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      
}

这是我的数据:

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

但是您可以看到-xItems是"dd.mm"格式的日期.由于它们是字符串,因此彼此之间具有相同的填充.我希望他们对实际日期更加准确.例如19.09和20.09应该非常接近.我知道我应该每天匹配一些数字才能完成它.但是我不知道下一步该怎么做-如何调整x标签边距?

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?

更新 经过小型研究后,我发现许多开发人员都询问了此功能,但是什么都没有发生-就我而言,我在Swift中找到了该库的非常有趣的替代品-

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.

推荐答案

最简单的解决方案是遍历您的数据,并为每个缺少的日期添加一个值为0的ChartDataEntry和相应的标签.

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.

为回应评论中的问题,以下是我的一个应用程序的屏幕截图,其中我使用0值填充了日期间隔:

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:

在我的情况下,我想要0值而不是从数据点到数据点的平均线,因为它清楚地表明跳过的日期没有数据(例如8/11).

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).

从@Philipp Jahoda的评论中看来,您可以跳过0值条目,而只需将所需的数据索引到正确的标签即可.

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.

我修改了MPAndroidChart示例程序,以跳过一些数据点,这就是结果:

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

正如@Philipp Jahoda在评论中提到的那样,该图表仅通过连接到下一个数据点来处理丢失的Entry.从下面的代码中,您可以看到我正在为整个数据集生成x值(标签),但是为索引11-29跳过了y值(数据点),这正是您想要的.剩下的唯一事情就是处理x标签,因为听起来好像您不想在我的示例中显示15、20和25.

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));
}

这篇关于使用值和日期制作折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆