getChart method

LineChartData getChart(
  1. dynamic context,
  2. List<LineChartPoint> series1,
  3. List<LineChartPoint>? series2
)

Implementation

LineChartData getChart(context, List<LineChartPoint> series1, List<LineChartPoint>? series2) {
  return LineChartData(
    gridData: FlGridData(
      show: true,
      drawVerticalLine: true,
      getDrawingHorizontalLine: (value) {
        int modulo = 8;

        var draw = value % modulo == 0;

        return FlLine(
          color: Colors.black12,
          strokeWidth: draw ? 1 : 0,
        );
      },
      getDrawingVerticalLine: (value) {
        int modulo = 2;

        var draw = value % modulo != 0;

        return FlLine(
          color: Colors.black12,
          strokeWidth: draw ? 1 : 0,
        );
      },
    ),
    titlesData: FlTitlesData(
      show: true,
      bottomTitles: SideTitles(
        showTitles: true,
        reservedSize: 22,
        getTextStyles: (value) => Theme.of(context).textTheme.caption!,
        getTitles: (value) {
          if (value == series1.first.x) return 'Last 30 days';
          if (value == series1.last.x) return 'Today';
          int modulo = 4;
          if (series1.length <= modulo || value % modulo != 0) return '';
          return value.toString();
        },
        margin: 8,
      ),
      leftTitles: SideTitles(
        showTitles: true,
        getTextStyles: (value) => Theme.of(context).textTheme.caption!,
        getTitles: (value) {
          int modulo = 8;

          if (series1.length <= modulo || value % modulo != 0) return '';

          return ySuffix + value.toString() + yAppendix;
        },
        reservedSize: 28,
        margin: 12,
      ),
    ),
    borderData: FlBorderData(
      show: true,
      border: Border.all(
        color: Colors.black12,
        width: 1,
      ),
    ),
    minX: minX ?? <LineChartPoint>[...series1, ...series2 ?? []].map((e) => e.x).reduce(min),
    maxX: maxX ?? <LineChartPoint>[...series1, ...series2 ?? []].map((e) => e.x).reduce(max),
    minY: minY ?? <LineChartPoint>[...series1, ...series2 ?? []].map((e) => e.y).reduce(min),
    maxY: maxY ?? <LineChartPoint>[...series1, ...series2 ?? []].map((e) => e.y).reduce(max),
    lineBarsData: [
      LineChartBarData(
        spots: series2?.map<FlSpot>((e) => FlSpot(e.x, e.y)).toList() ?? [],
        isCurved: true,
        colors: [purple],
        barWidth: 2,
        isStrokeCapRound: false,
        dotData: FlDotData(
          show: true,
          getDotPainter: (a, b, c, d) {
            return FlDotCirclePainter(
              radius: 2.5,
              strokeWidth: 2,
              strokeColor: purple,
              color: Colors.white,
            );
          },
        ),
        belowBarData: BarAreaData(
          show: true,
          gradientFrom: Offset(0, 0),
          gradientTo: Offset(0, 2),
          colors: gradientColors2.map((color) => color.withOpacity(0.3)).toList(),
        ),
      ),
      LineChartBarData(
        spots: series1.map<FlSpot>((e) => FlSpot(e.x, e.y)).toList(),
        isCurved: true,
        colors: [darkBlue],
        barWidth: 2,
        isStrokeCapRound: false,
        dotData: FlDotData(
          show: true,
          getDotPainter: (a, b, c, d) {
            return FlDotCirclePainter(
              radius: 2.5,
              strokeWidth: 2,
              strokeColor: darkBlue,
              color: Colors.white,
            );
          },
        ),
        belowBarData: BarAreaData(
          show: true,
          gradientFrom: Offset(0, 0),
          gradientTo: Offset(0, 2),
          colors: gradientColors.map((color) => color.withOpacity(0.3)).toList(),
        ),
      ),
    ],
  );
}