将 Highcharts(和模块)与 lit-element 一起使用

Using Highcharts (and modules) with lit-element

根据GitHub Issue,这是一个后续问题:

我想将 Highcharts 和一些模块与 Lit-Element 一起使用,但在导入 Highcharts 和模块时遇到困难。

如果我使用import 'highcharts';,我可以完美地在我的代码中使用Highcharts-Object。使用给定的解决方案 (import * as Highcharts from 'highcharts';),它不起作用。

任何给定的模块导入解决方案都不起作用:

import 'highcharts/modules/exporting'; // doesn't throw an error, but can't bind it to Highcharts

import HC_exporting from 'highcharts/modules/exporting'; // does throw a 'no named default export' error

import * as HC_exporting from 'highcharts/modules/exporting'; // throws 'TypeError: HC_exporting is not a function'
ld-application-actions.js:56 Uncaught (in promise) SyntaxError: The requested module '../../../../node_modules/highcharts/modules/exporting.js' does not provide an export named 'default'

那么有没有可能导入和使用 Highcharts 及其模块?

疯狂的事情:我试图在 Stackblitz 上创建一个示例,但它确实有效:https://stackblitz.com/edit/ic7f4z

这里有什么区别?那是因为 Stackblitz 使用 TypeScript 导入,而我使用 polyserve 而不使用 TypeScript?

更新:

我在 JSFiddle 上创建了相同的示例(参见 https://jsfiddle.net/sebastianroming/uer59cnw/6/),这里与我的机器上的相同:不起作用。随时取消注释注释行以获得控制台的输出。

谢谢!

您的 tsconfig.json 需要包括:

"esModuleInterop"             : true,
"allowSyntheticDefaultImports": true,

参见:https://www.typescriptlang.org/docs/handbook/compiler-options.html

以这种方式配置 TypeScript 后,应该可以执行以下操作:

import * as HC_exporting from 'highcharts/modules/exporting';

根据文档,导出库称为 Exporting: https://api.highcharts.com/class-reference/#toc7

import Highcharts from 'highcharts';
// Alternatively, this is how to load Highstock. Highmaps and Highcharts Gantt are similar.
// import Highcharts from 'highcharts/highstock';

// Load the exporting module.
import Exporting from 'highcharts/modules/exporting';
// Initialize exporting module. (CommonJS only)
Exporting(Highcharts);

// Generate the chart
Highcharts.chart('container', {
  // options - see https://api.highcharts.com/highcharts
});

由于您使用的是 Lit Element,因此我假设您也安装了 Webpack,这允许您以不同的方式使用它,如下所示: https://gist.github.com/jon-a-nygaard/f22ade93209277eea5b57c0f6ca51ea7

使用 lit-elementpolyserve Highcharts 及其模块可以这样下载:

import 'highcharts';
import 'highcharts/modules/exporting';
import 'highcharts/modules/boost'
import 'highcharts/highcharts-more';

组件代码:

import { LitElement, html } from 'lit-element';

import 'highcharts';
import 'highcharts/modules/exporting';
import 'highcharts/modules/boost'
import 'highcharts/highcharts-more';

class ChartTest extends LitElement {

  firstUpdated(changedProperties) {
    this._enableChart();
  }

  render() {
    return html`
      <div id='container' style='width:500px;height:300px;border: 1px red solid;'>sfsdfs</div>
    `;
  }

  _enableChart() {
    Highcharts.chart(this.shadowRoot.querySelector('#container'), {
      xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
        ]
      },
      series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
        type: 'column'
      }]
    });
  }

}
customElements.define('chart-test', ChartTest);

演示:

您也可以使用 highcharts webcomponent。这是 highcharts 的便利包装。 (其内部使用 Lit 实现)