animate method
void
animate()
If you wish to perform initial animation again in the existing series, this method can be called.
On calling this method, this particular series will be animated again based on the animationDuration
property's value in the series. If the value is 0, then the animation will not be performed.
Widget build(BuildContext context) {
ChartSeriesController? _chartSeriesController;
return Column(
children: <Widget>[
SfCartesianChart(
series: <LineSeries<SalesData, num>>[
LineSeries<SalesData, num>(
onRendererCreated: (ChartSeriesController controller) {
_chartSeriesController = controller;
},
),
]
),
TextButton(
child: Text("Animate series"),
onPressed: () {
_chartSeriesController?.animate();
}
)
]
);
}
Implementation
void animate() {
final SeriesRendererDetails seriesRendererDetails =
SeriesHelper.getSeriesRendererDetails(seriesRenderer);
if (seriesRendererDetails.visible! == true &&
seriesRendererDetails.series.animationDuration > 0 == true) {
final CartesianStateProperties stateProperties =
seriesRendererDetails.stateProperties;
final SfCartesianChart chart =
seriesRendererDetails.stateProperties.chart;
final TooltipBehavior tooltip = chart.tooltipBehavior;
final TrackballBehavior trackball = chart.trackballBehavior;
final TrackballRenderingDetails trackballRenderingDetails =
TrackballHelper.getRenderingDetails(
stateProperties.trackballBehaviorRenderer);
final TrackballPainter? trackballPainter =
trackballRenderingDetails.trackballPainter;
final RenderingDetails renderingDetails =
stateProperties.renderingDetails;
final TooltipRenderingDetails tooltipRenderingDetails =
TooltipHelper.getRenderingDetails(
renderingDetails.tooltipBehaviorRenderer);
//This hides the tooltip if rendered for this current series renderer
if (tooltip.enable &&
(tooltip.builder != null
? tooltipRenderingDetails.seriesIndex ==
SegmentHelper.getSegmentProperties(
seriesRendererDetails.segments[0])
.seriesIndex
: tooltipRenderingDetails.currentSeriesDetails?.renderer ==
seriesRenderer)) {
tooltip.hide();
}
// This hides the trackball if rendered for this current series renderer.
if (trackball.enable) {
for (final ChartPointInfo point
in trackballRenderingDetails.chartPointInfo) {
if (point.seriesRendererDetails?.renderer == seriesRenderer) {
if (trackballPainter != null) {
stateProperties.repaintNotifiers['trackball']!.value++;
trackballPainter.canResetPath = true;
break;
} else {
final GlobalKey key =
trackballRenderingDetails.trackballTemplate!.key as GlobalKey;
final TrackballTemplateState trackballTemplateState =
key.currentState! as TrackballTemplateState;
trackballTemplateState.hideTrackballTemplate();
break;
}
}
}
}
seriesRendererDetails.reAnimate = seriesRendererDetails.needsAnimation =
seriesRendererDetails.needAnimateSeriesElements = true;
renderingDetails.initialRender = false;
// This repaints the datalabels for the series if renderered.
stateProperties.renderDataLabel?.state?.repaintDataLabelElements();
// This animates the datalabel templates of the animating series.
if (seriesRendererDetails.series.dataLabelSettings.builder != null) {
for (final ChartTemplateInfo template in renderingDetails.templates) {
if (template.templateType == 'DataLabel' &&
template.animationDuration > 0 &&
template.seriesIndex ==
SegmentHelper.getSegmentProperties(
seriesRendererDetails.segments[0])
.seriesIndex) {
template.animationController.forward(from: 0.0);
}
}
}
stateProperties.totalAnimatingSeries = 1;
stateProperties.animationCompleteCount = 0;
stateProperties.forwardAnimation(seriesRendererDetails);
// This animates the trendlines of the animating series.
if (seriesRendererDetails.trendlineRenderer.isNotEmpty == true) {
for (final TrendlineRenderer trendlineRenderer
in seriesRendererDetails.trendlineRenderer) {
if (trendlineRenderer.visible) {
final Trendline trendline = trendlineRenderer.trendline;
trendlineRenderer.animationController.duration =
Duration(milliseconds: trendline.animationDuration.toInt());
trendlineRenderer.animationController.forward(from: 0.0);
}
}
}
}
}