问题描述
有了这张图表,我该如何添加红线(现在)?
这是我目前所拥有的:
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,);