使用 fl_chart 我们如何添加现在行?

问题描述

有了这张图表,我该如何添加红线(现在)?

enter image description here

这是我目前所拥有的:

LineChartData(
  maxY: maxY + 10 * _onePercentRange,minY: minY - 10 * _onePercentRange,titlesData: FlTitlesData(
    bottomTitles: SideTitles(
      showTitles: true,margin: 10,getTitles: (value) {
        return getDateBaseinformatFromJsonString(
            dates[value.toInt()],"MM/dd\nHHmm");
      },),borderData: FlBorderData(
    show: true,border: Border(
      bottom: BorderSide(
        color: currentTheme.primaryColor,width: 2,left: BorderSide(
        color: currentTheme.primaryColor,lineBarsData: [
    LineChartBarData(
      spots: spots,isCurved: true,colors: [currentTheme.primaryColor],isstrokeCapRound: true,dotData: FlDotData(
        show: false,],);

解决方法

这是我的解决方案:

List<int> dates = tideData
    .map(
      (e) => e.timeStamp,)
    .toList();

double? nowScaleDouble;

var nowTimeStamp = DateTime.now().millisecondsSinceEpoch;
for (var i = 1; i < dates.length; i++) {
  if (dates[i - 1] <= nowTimeStamp && dates[i] >= nowTimeStamp) {
    var _intervalLength = dates[i] - dates[i - 1];
    var _x = dates[i] - nowTimeStamp;
    var _percent = _x / _intervalLength;
    nowScaleDouble = i + _percent;
    break;
  }
}

return LineChartData(
  extraLinesData: ExtraLinesData(
    verticalLines: nowScaleDouble == null
        ? null
        : [
            VerticalLine(
              x: nowScaleDouble,color: const Color.fromRGBO(197,1),strokeWidth: 2,dashArray: [5,10],label: VerticalLineLabel(
                show: true,alignment: Alignment(1,0.5),padding: const EdgeInsets.only(left: 10,top: 5),labelResolver: (line) =>
                    dateToScreen(DateTime.now(),format: "MM/dd\nHHmm"),),],maxY: maxY + 10 * _onePercentRange,minY: minY - 10 * _onePercentRange,titlesData: FlTitlesData(
    bottomTitles: SideTitles(
      showTitles: true,margin: 10,getTitles: (value) {
        return dateToScreen(
            DateTime.fromMillisecondsSinceEpoch(
              dates[value.toInt()],format: "MM/dd\nHHmm");
      },borderData: FlBorderData(
    show: true,border: Border(
      bottom: BorderSide(
        color: currentTheme.primaryColor,width: 2,left: BorderSide(
        color: currentTheme.primaryColor,lineBarsData: [
    LineChartBarData(
      spots: spots,isCurved: true,colors: [currentTheme.primaryColor],isStrokeCapRound: true,dotData: FlDotData(
        show: false,);

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...