【问题标题】:Dart DivElement飞镖 DivElement
【发布时间】:2019-03-02 07:45:10
【问题描述】:

我想在 Div 中插入我的图表。在 HTML 中,我有一个像这样的 div 元素

<div id="barchart_values" style="width: 900px; height: 300px;"></div>

在 dart 中,我正在创建一个这样的容器。

Element createContainer() {
var e = new DivElement()
  ..style.height = '300px'
  ..style.maxWidth = '70%'
  ..style.marginBottom = '50px';
document.body.append(e);
return e;
}

函数 createContainer 工作正常,如上...我想在“barchart_values”div 内准确地绘制图表。我尝试如下所示,但它不起作用

Element createContainer() {
 var e = querySelector('barchart_values');
 document.body.append(e);
 return e;
}

谁能告诉我如何在 div 元素 'barchart_values' 中添加图表

我的条形图代码

import 'package:modern_charts/modern_charts.dart';
BarChart chart;
DataTable table;  
table = new DataTable([
    ['Categories', 'Current', 'X1', 'X2', 'X3'],
    ['Value', 0, 0, 0, 0],
    ['', null, null, null, null],
  ]);



  var container1 = createContainer();

  var options = {
    'animation': {
      'onEnd': () {

      }
    },
    'series': {
      'labels': {'enabled': true}
    },
    'xAxis': {
      'crosshair': {'enabled': true},
      'labels': {'maxRotation': 90, 'minRotation': 0}
    },
    'yAxis': {'maxValue': 20, 'minInterval': 5},
    'title': {'text': 'Values'},
    'tooltip': {'valueFormatter': (value) => '$value core'}
  };

  chart = new BarChart(container1);
  chart.draw(table, options);
  //// end of chart creation

这是我遇到的错误

NullError: 找不到方法: 'toString' on null 原因:空 堆栈跟踪: [TypeError:无法在 BarChart.Chart$1 处读取 null 的属性“toString”

【问题讨论】:

  • 不清楚是什么问题。除了 id 字符串之外,您的代码中似乎没有任何内容与条形图相关。
  • 我添加了图表编码
  • 是的,只有我创建了它工作正常的图表。问题是 Element createContainer() { var e = new DivElement() ..style.height = '400px' // ..style.width = '800px' ..style.maxWidth = '100%' ..style.marginBottom ='50px'; document.body.append(e);返回 e;由于这个功能,图表被附加在网页的底部。相反,我想要这个 div
    中的图表
  • 我试过 var e = document.getElementById('barchart_values');它没有用。我试过 var e = querySelector('barchart_values');它没有用。

标签: dart dart-html


【解决方案1】:

将此添加到您的元素中

<div id="barchart_values" style="width: 900px; height: 300px;"></div>

createContainer 一样

Element createContainer() {
 return getElementById('barchart_values');
}

【讨论】:

  • 不,它没有用我试过 return getElementById('barchart_values');没用我试过 return document.getElementById('barchart_values');没用我试过 return document.getElementById('#barchart_values');没用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2015-07-18
  • 2019-06-29
  • 2021-10-11
  • 2019-05-14
相关资源
最近更新 更多