1

有了这个图表,我怎样才能添加红线(现在)?

在此处输入图像描述

这是我到目前为止所拥有的:

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,
        width: 2,
      ),
    ),
  ),
  lineBarsData: [
    LineChartBarData(
      spots: spots,
      isCurved: true,
      colors: [currentTheme.primaryColor],
      isStrokeCapRound: true,
      dotData: FlDotData(
        show: false,
      ),
    ),
  ],
);
4

1 回答 1

1

这是我的解决方案:

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, 0, 0, 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,
        width: 2,
      ),
    ),
  ),
  lineBarsData: [
    LineChartBarData(
      spots: spots,
      isCurved: true,
      colors: [currentTheme.primaryColor],
      isStrokeCapRound: true,
      dotData: FlDotData(
        show: false,
      ),
    ),
  ],
);

在此处输入图像描述

于 2021-07-15T01:18:19.343 回答