0

I have included https://code.highcharts.com/highcharts.js as static resource (in format text/javascript) in my org.

When I tried a sample demo code, there is always a message saying .chart is not a function. I am thinking maybe I did not load the Highcharts JavaScript correctly. But I have no idea how to modify it.

Here is how my js file in lwc looks like:

import { LightningElement } from "lwc";
import { loadScript} from "lightning/platformResourceLoader";

import jqeuryUI from "@salesforce/resourceUrl/jquery_ui_1_12_1";
import jquery from "@salesforce/resourceUrl/jquery_2_2_4";
import Highcharts from "@salesforce/resourceUrl/highcharts";

export default class CDP_trial extends LightningElement {

  renderedCallback() {
    Promise.all([
      loadScript(this, jquery)
        .then(() => console.log("jQuery loaded"))
        .catch(error => console.log("Error in loading jQuery")),

      loadScript(this, jqeuryUI + "/jquery-ui-1.12.1/jquery-ui.min.js")
        .then(() => console.log("jqeuryUI loaded"))
        .catch(error => console.log("Error in loading jqeuryUI")),

      loadScript(this, Highcharts)
        .then(() => console.log("Highcharts loaded"))
        .catch(error => console.log("Error in loading Highcharts"))
    ])

      .then(() => {
        this.runHighcharts();
      })

      .catch(error => {
        window.console.log("The error is: " + error);
      });
  }

  runHighcharts() {

    Highcharts.chart("container", {
      title: {
        text: "Logarithmic axis demo"
      },

      xAxis: {
        tickInterval: 1,
        type: "logarithmic",
        accessibility: {
          rangeDescription: "Range: 1 to 10"
        }
      },

      yAxis: {
        type: "logarithmic",
        minorTickInterval: 0.1,
        accessibility: {
          rangeDescription: "Range: 0.1 to 1000"
        }
      },

      tooltip: {
        headerFormat: "<b>{series.name}</b><br />",
        pointFormat: "x = {point.x}, y = {point.y}"
      },

      series: [
        {
          data: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512],
          pointStart: 1
        }
      ]
    });
  }
}

And html is as follow:

<template>
    <div id="container"></div>    
</template>

1 Answer 1

2

You have a name collision, so it's not surprising you'd run into problems. Change your resource constants:

import HIGHCHARTS from "@salesforce/resourceUrl/highcharts";

...

loadScript(this, HIGHCHARTS)

As an aside, you need lwc:dom="manual" to enable access to the DOM:

<div id="container" lwc:dom="manual"></div>    

Also, the id will invariably be modified, so you'll need to get the id dynamically:

const containerId = this.template.querySelector('div').id;

This happens because the framework generates unique ID values for each element to avoid DOM id collisions.

7
  • Thank you sfdcfox. But doing what you mentioned above, I still get Highcharts error #13: www.highcharts.com/errors/13/. It seems the DOM id still can not be found. Here is what Highcharts describes error #13: "Rendering div not found. This error occurs if the chart.renderTo option is misconfigured so that Highcharts is unable to find the HTML element to render the chart in. If using a DOM ID when creating the chart, make sure a node with the same ID exists somewhere in the DOM." Commented Mar 25, 2020 at 3:51
  • 3
    @ZhuangPaulus Checking the docs, looks like you can pass in the div directly: Highcharts.chart(this.template.querySelector('div'), { (adjust as necessary to query the correct DOM element).
    – sfdcfox
    Commented Mar 25, 2020 at 3:55
  • you have solved what I have been stuck with for two days in a few minutes... Thank you so much!!!!!! Commented Mar 25, 2020 at 4:04
  • 1
    @ZhuangPaulus You're welcome! Glad I was able to get you going. Be well!
    – sfdcfox
    Commented Mar 25, 2020 at 4:11
  • Hi @sfdcfox, I encountered another problem. I was trying to load the script of this one "code.highcharts.com/modules/solid-gauge.src.js" in order to see solid gauge. It succeeded the first time I made the request in promise all. But after that it kept on reporting errors in loading this script. What might be the reason? I can only see that chart each time I remove and "New" solid-gauge.src.js one more time in the static resource. Commented Mar 26, 2020 at 2:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .