-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Is it possible to add marker/tooltip on line chart and bar chart? #58
Comments
We don't yet have a timeline for adding touch cards. |
@oneeall did you find a solution to add the label where the user has selected a point in the graph ? |
Is this issue resolved? |
any news about this? |
It can be done, although it took me some time to figure out. Now for the tricky part: I extended Maybe in the future the authors will make this easier but for now you use this method to modify rendering process of the selected point in any way you need (using EDIT: Since my initial comment I have received several questions asking for a deeper explanation so I put together an example widget class: Expand flutter chart label exampleimport 'dart:math';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart';
import 'package:charts_flutter/src/text_element.dart';
import 'package:charts_flutter/src/text_style.dart' as style;
class Chart extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LineChart(
_createSampleData(),
behaviors: [
LinePointHighlighter(
symbolRenderer: CustomCircleSymbolRenderer()
)
],
selectionModels: [
SelectionModelConfig(
changedListener: (SelectionModel model) {
if(model.hasDatumSelection)
print(model.selectedSeries[0].measureFn(model.selectedDatum[0].index));
}
)
],
);
}
List<Series<LinearSales, int>> _createSampleData() {
final data = [
new LinearSales(0, 5),
new LinearSales(1, 25),
new LinearSales(2, 100),
new LinearSales(3, 75),
];
return [
new Series<LinearSales, int>(
id: 'Sales',
colorFn: (_, __) => MaterialPalette.blue.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.sales,
data: data,
)
];
}
}
class CustomCircleSymbolRenderer extends CircleSymbolRenderer {
@override
void paint(ChartCanvas canvas, Rectangle<num> bounds, {List<int> dashPattern, Color fillColor, Color strokeColor, double strokeWidthPx}) {
super.paint(canvas, bounds, dashPattern: dashPattern, fillColor: fillColor, strokeColor: strokeColor, strokeWidthPx: strokeWidthPx);
canvas.drawRect(
Rectangle(bounds.left - 5, bounds.top - 30, bounds.width + 10, bounds.height + 10),
fill: Color.white
);
var textStyle = style.TextStyle();
textStyle.color = Color.black;
textStyle.fontSize = 15;
canvas.drawText(
TextElement("1", style: textStyle),
(bounds.left).round(),
(bounds.top - 28).round()
);
}
}
class LinearSales {
final int year;
final int sales;
LinearSales(this.year, this.sales);
} |
@stasgora But how to display the value, not the fixed "1"? |
@joaquinperaza just create simple one variable model, pass it to the |
Thank you! |
I've tried your code, seems it working for hardcoded String. I wrap
Then, the custom circle appear and disappear automatically in 1s. This my constructor in
This is the behaviour looks like: https://imgur.com/8CwRKme |
Yes, it does that. It is due to the fact that |
I tried to use these two combination of behaviors,
It seems crash. Crash log: https://pastebin.com/4i8x30tG |
Sir |
ok i figured out. |
@pockyzhang 什么意思,能给个完整的页面demo吗 |
@stasgora |
Thanks for the comment. But it didn't worked for me. render.text always get the right value (inside setState()), but the tooltip never appear. |
The graph should show the label when it is displayed. No display is meaningless to the user. |
Moreover, there is no Bezier curve. |
For Bar Graph, I hv implemented my custom label decorator which display labels on vertical bar chart: To Use: |
I have done the same thing but the value of the text is not updating, please help me |
@stasgora Great work! Now I have two or more series in one chart. This will show symbols near all series. I wanna get all the data from each series into one variable. And only show one tooltip near one of the series. How can I achieve that? |
I am essentially looking for the same, I have multiple series? any luck figuring this out? I am using the selection callback to just display the values in a card below the chart at the moment. |
What about showing the values when you click on the graph? I have done this |
Have you done with tooltips for chart? |
English pls, no one can understand |
How to do this ? |
you can post full example? |
Can you please post full example? |
saved me, thank you very much! |
@jeffersonmello sorry, I'm not use this tool just now. For me, flutter_echarts is more friendly. |
|
I added tooltip but is there a way to hide it? Also it seems that there is some problem when SlidingViewport is enabled. |
Hi tried the solutions given by everyone but in bar charts having two sets of data the tooltip gets drawn twice . I am attaching a screenshot below for reference. Can anyone help me solve it. Code :
CustomSymbolRenderer :
|
The issue is open since 2018. Is it fixed in the recent release? |
Could you share an example? |
@randhika any solution for your problem? |
@randhika @AmalinaMakhtar |
@Renatinaveen You are missing there CustomRectangleSymbolRenderer piece of code. |
@M1chlCZ thanks. It's updated please check it out. |
This is the full example: test_page.dart import 'dart:math';
import 'package:flutter/painting.dart' as painting;
import 'package:charts_flutter/flutter.dart';
import 'package:f_wallet/widget/text_symbol_renderer.dart';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts hide Color;
class TestModePage extends StatefulWidget {
const TestModePage({Key? key}) : super(key: key);
@override
State<TestModePage> createState() => _TestModePageState();
}
class _TestModePageState extends State<TestModePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('TestMode'),
),
body: Padding(
padding: const EdgeInsets.all(16),
child: AspectRatio(
aspectRatio: 1,
child: Container(
padding: const EdgeInsets.all(16),
child: PointsLineChart.withSampleData(),
decoration: const ShapeDecoration(
color: painting.Color(0xFFDBE1F1),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16))),
),
)
),
),
);
}
}
class PointsLineChart extends StatelessWidget {
final List<charts.Series<LinearSales, int>> seriesList;
final bool animate;
PointsLineChart(this.seriesList, {required this.animate});
/// Creates a [LineChart] with sample data and no transition.
factory PointsLineChart.withSampleData() {
return new PointsLineChart(
_createSampleData(),
// Disable animations for image tests.
animate: false,
);
}
@override
Widget build(BuildContext context) {
return new charts.LineChart(seriesList,
animate: animate,
defaultRenderer: new charts.LineRendererConfig(includePoints: true),
behaviors: [
charts.LinePointHighlighter(
////////////////////// notice ////////////////////////////
symbolRenderer: TextSymbolRenderer(() => Random().nextInt(100).toString()),
////////////////////// notice ////////////////////////////
),
],
);
}
/// Create one series with sample hard coded data.
static List<charts.Series<LinearSales, int>> _createSampleData() {
final data = [
new LinearSales(0, 5),
new LinearSales(1, 25),
new LinearSales(2, 100),
new LinearSales(3, 75),
];
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.sales,
data: data,
)
];
}
}
/// Sample linear data type.
class LinearSales {
final int year;
final int sales;
LinearSales(this.year, this.sales);
} text_symbol_render.dart import 'dart:developer' as developer;
import 'dart:math';
import 'package:charts_flutter/flutter.dart';
import 'package:charts_flutter/src/text_style.dart' as style;
import 'package:charts_flutter/src/text_element.dart' as element;
import 'package:flutter/material.dart';
typedef GetText = String Function();
class TextSymbolRenderer extends CircleSymbolRenderer {
TextSymbolRenderer(this.getText, {this.marginBottom = 8, this.padding = const EdgeInsets.all(8)});
final GetText getText;
final double marginBottom;
final EdgeInsets padding;
@override
void paint(ChartCanvas canvas, Rectangle<num> bounds, {List<int>? dashPattern, Color? fillColor, FillPatternType? fillPattern, Color? strokeColor, double? strokeWidthPx}) {
super.paint(canvas, bounds, dashPattern: dashPattern, fillColor: fillColor, fillPattern: fillPattern, strokeColor: strokeColor, strokeWidthPx: strokeWidthPx);
style.TextStyle textStyle = style.TextStyle();
textStyle.color = Color.black;
textStyle.fontSize = 15;
element.TextElement textElement = element.TextElement(getText.call(), style: textStyle);
double width = textElement.measurement.horizontalSliceWidth;
double height = textElement.measurement.verticalSliceWidth;
double centerX = bounds.left + bounds.width / 2;
double centerY = bounds.top + bounds.height / 2 - marginBottom - (padding.top + padding.bottom);
canvas.drawRRect(
Rectangle(
centerX - (width / 2) - padding.left,
centerY - (height / 2) - padding.top,
width + (padding.left + padding.right),
height + (padding.top + padding.bottom),
),
fill: Color.white,
radius: 16,
roundTopLeft: true,
roundTopRight: true,
roundBottomRight: true,
roundBottomLeft: true,
);
canvas.drawText(
textElement,
(centerX - (width / 2)).round(),
(centerY - (height / 2)).round(),
);
}
} |
@YorekLiu I tried it and it works but when I'm getting the actual data the first element of the list from changedListener using bloc and trying to pass it to the text renderer it doesn't appear on the chart anymore while the string in bloc has the data |
i want to make the same chart in flutter to show yearly activity on the base of data like this |
@YorekLiu points are overlaid over the numbers |
@ljmatan Could you please share code example? |
@cheadevit https://github.com/ljmatan/mingo There's no API in there so you won't be able to compile it though, you can take a look at the chart_section.dart file and YorekLiu's comment. |
I have intention on looking for a specific point in a line chart of charts_flutter and then display the the marker to highlight the point after a points or bar is hit.
My question is, Is it possible to create a widget or add some label to the line chart and bar chart?
The text was updated successfully, but these errors were encountered: