imaNNeo / fl_chart

FL Chart is a highly customizable Flutter chart library that supports Line Chart, Bar Chart, Pie Chart, Scatter Chart, and Radar Chart.
https://flchart.dev
MIT License
6.77k stars 1.74k forks source link

added custom properties attribute to line chart bar data #1664

Open alr2413 opened 4 months ago

alr2413 commented 4 months ago

Hi, thanks for providing this awesome library. In this PR, I added a properties attribute as Map<string, dynamic> in order to store optional line curve parameters, like name or any other curve setting. by this change if you have multiple curves in a chart, now it's possible to show the name of each curve next to the value on the tooltip message. image

alr2413 commented 3 months ago

@imaNNeo, is there any issue with this PR? please let me know, thanks.

imaNNeo commented 3 months ago

Yes, there are issues with this PR,

  1. I think it is better to use dynamic or Object type for the properties
  2. I think you can wrap the LineChartBarData to add your own custom data, you can check out this message
  3. Please read the contributing guidelines, (you should not change the library version)
alr2413 commented 3 months ago

Thanks for your reply :) Here is my comments:

  1. I defined the type of properties attribute, because if we want to include that attribute in lerp operation, the type must be known. so we can't use the dynamic or Object type.

  2. I think it would be the best if you include this hint in readme file (since currently there are multiple PR requests for this types of features). I tried to do that and it worked. Just need to override the copyWith operation. I added here as a reference:

class LineChartBarDataWrapper extends LineChartBarData {
  final String label;

  LineChartBarDataWrapper({
    super.spots,
    super.show,
    super.color,
    super.gradient,
    super.barWidth,
    super.isCurved,
    super.curveSmoothness,
    super.preventCurveOverShooting,
    super.preventCurveOvershootingThreshold,
    super.isStrokeCapRound,
    super.isStrokeJoinRound,
    super.belowBarData,
    super.aboveBarData,
    super.dotData,
    super.showingIndicators,
    super.dashArray,
    super.shadow,
    super.isStepLineChart,
    super.lineChartStepData,
    required this.label,
  });

  @override
  LineChartBarDataWrapper copyWith({
    List<FlSpot>? spots,
    bool? show,
    Color? color,
    Gradient? gradient,
    double? barWidth,
    bool? isCurved,
    double? curveSmoothness,
    bool? preventCurveOverShooting,
    double? preventCurveOvershootingThreshold,
    bool? isStrokeCapRound,
    bool? isStrokeJoinRound,
    BarAreaData? belowBarData,
    BarAreaData? aboveBarData,
    FlDotData? dotData,
    List<int>? dashArray,
    List<int>? showingIndicators,
    Shadow? shadow,
    bool? isStepLineChart,
    LineChartStepData? lineChartStepData,
    String? label,
  }) {
    return LineChartBarDataWrapper(
      spots: spots ?? this.spots,
      show: show ?? this.show,
      color: color ?? this.color,
      gradient: gradient ?? this.gradient,
      barWidth: barWidth ?? this.barWidth,
      isCurved: isCurved ?? this.isCurved,
      curveSmoothness: curveSmoothness ?? this.curveSmoothness,
      preventCurveOverShooting:
          preventCurveOverShooting ?? this.preventCurveOverShooting,
      preventCurveOvershootingThreshold: preventCurveOvershootingThreshold ??
          this.preventCurveOvershootingThreshold,
      isStrokeCapRound: isStrokeCapRound ?? this.isStrokeCapRound,
      isStrokeJoinRound: isStrokeJoinRound ?? this.isStrokeJoinRound,
      belowBarData: belowBarData ?? this.belowBarData,
      aboveBarData: aboveBarData ?? this.aboveBarData,
      dotData: dotData ?? this.dotData,
      dashArray: dashArray ?? this.dashArray,
      showingIndicators: showingIndicators ?? this.showingIndicators,
      shadow: shadow ?? this.shadow,
      isStepLineChart: isStepLineChart ?? this.isStepLineChart,
      lineChartStepData: lineChartStepData ?? this.lineChartStepData,
      label: label ?? this.label,
    );
  }
}