gpt4 book ai didi

flutter - 如何更改charts_flutter条形图中条形图的宽度?

转载 作者:行者123 更新时间:2023-12-04 10:58:00 24 4
gpt4 key购买 nike

正在尝试使用 charts_flutter用于条形图,但找不到用于减少简单条形图中条形宽度的属性。

以下是我的模拟:
charts

另外,不知道如何:

- 在此包中添加以下索引

- 在条形内添加值,如模拟中所示。

以下是我的代码:

class HiddenTicksAndLabelsAxis extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;

HiddenTicksAndLabelsAxis(this.seriesList, {this.animate});

factory HiddenTicksAndLabelsAxis.withSampleData() {
return new HiddenTicksAndLabelsAxis(
_createSampleData(),
// Disable animations for image tests.
animate: true,
);
}


@override
Widget build(BuildContext context) {
return new charts.BarChart(
seriesList,
animate: animate,

/// Assign a custom style for the measure axis.
///
/// The NoneRenderSpec can still draw an axis line with
/// showAxisLine=true.
primaryMeasureAxis:
new charts.NumericAxisSpec(renderSpec: new charts.NoneRenderSpec()),

/// This is an OrdinalAxisSpec to match up with BarChart's default
/// ordinal domain axis (use NumericAxisSpec or DateTimeAxisSpec for
/// other charts).
domainAxis: new charts.OrdinalAxisSpec(
// Make sure that we draw the domain axis line.
showAxisLine: true,
// But don't draw anything else.
renderSpec: new charts.NoneRenderSpec()),
);
}

/// Create series list with single series
static List<charts.Series<OrdinalSales, String>> _createSampleData() {
final globalSalesData = [
new OrdinalSales('2014', 3, Colors.lightGreen),
new OrdinalSales('2015', 6,Colors.lightBlue),
new OrdinalSales('2016', 0.5,Colors.red),
// new OrdinalSales('2017', 750000),
];

return [
new charts.Series<OrdinalSales, String>(
id: 'Global Revenue',
domainFn: (OrdinalSales sales, _) => sales.year,
measureFn: (OrdinalSales sales, _) => sales.sales,
colorFn: (OrdinalSales sales, _) => sales.color,

data: globalSalesData,
),
];
}
}

/// Sample ordinal data type.
class OrdinalSales {
final String year;
final double sales;

final charts.Color color;

OrdinalSales(this.year, this.sales, Color color)
: this.color = new charts.Color(
r: color.red, g: color.green, b: color.blue, a: color.alpha);
}

我是新手,如果有任何问题,请原谅。

最佳答案

目前还没有设置条宽的功能。问题是开放的,见 Here .

您可以通过 barGroupingType: charts.BarGroupingType.grouped 最小化条宽并设置 fillColorFn: (Mileage mileage, _) => charts.ColorUtil.fromDartColor(Colors.transparent)charts.Series没有中间的。

我在下面分享我的代码:

输出:



代码:

custom_rounded_bars.dart 文件,

import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:easytrax/screens/analytics_page.dart';

class CustomRoundedBars extends StatelessWidget {
final List<dynamic> data;
final bool animate;

CustomRoundedBars(this.data, {this.animate});


@override
Widget build(BuildContext context) {

var seriesList = _createSampleData(data);

return new charts.BarChart(
seriesList,
animate: animate,
barGroupingType: charts.BarGroupingType.grouped,
defaultRenderer: new charts.BarRendererConfig(
cornerStrategy: const charts.ConstCornerStrategy(30)),
);
}

/// Create one series with sample hard coded data.
static List<charts.Series<Mileage, String>> _createSampleData(dynamic data) {

return [
new charts.Series<Mileage, String>(
id: 'mileage1',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (Mileage mileage, _) => mileage.day,
measureFn: (Mileage mileage, _) => mileage.km,
data: data,
fillColorFn: (Mileage mileage, _) => charts.ColorUtil.fromDartColor(Colors.transparent),
),
new charts.Series<Mileage, String>(
id: 'mileage',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (Mileage mileage, _) => mileage.day,
measureFn: (Mileage mileage, _) => mileage.km,
data: data,
),
new charts.Series<Mileage, String>(
id: 'mileage2',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (Mileage mileage, _) => mileage.day,
measureFn: (Mileage mileage, _) => mileage.km,
data: data,
fillColorFn: (Mileage mileage, _) => charts.ColorUtil.fromDartColor(Colors.transparent),
),
];
}

}

analytics_page.dart 文件,
import 'package:easytrax/Common/custom_rounded_bars.dart';
import 'package:flutter/material.dart';



class AnalyticsPage extends StatefulWidget {
static const String page = 'analytics_page';
@override
_AnalyticsPageState createState() => _AnalyticsPageState();
}

class _AnalyticsPageState extends State<AnalyticsPage> {

List<Mileage> _createSampleData(){
final data = [
new Mileage(10, 'April 27'),
new Mileage(38, 'April 28'),
new Mileage(60, 'April 29'),
new Mileage(20, 'April 30'),
new Mileage(45, 'May 01'),
new Mileage(35, 'May 02'),
new Mileage(55, 'May 03'),
];

return data;
}

@override
Widget build(BuildContext context) {


var data = _createSampleData();


return Scaffold(
body:Container(
child: Column(
children: [
Container(
padding: EdgeInsets.all(16.0),
height: 300.0,
child: CustomRoundedBars(data),
),
],
),
),
);
}
}



/// data type.
class Mileage {
final int km;
final String day;

Mileage(this.km, this.day);
}

关于flutter - 如何更改charts_flutter条形图中条形图的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59047870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com