Git Product home page Git Product logo

Comments (3)

IanG-21 avatar IanG-21 commented on May 23, 2024 1

This is relatively simple to achieve by extending the Panel ReactiveHTML class with the latest version of Panel. The trickier part is that Panel now uses a Shadow DOM, so needs some JS to work, something like this should work:

from panel.reactive import ReactiveHTML
import param

class PanelHighchart(ReactiveHTML):
    chart_js: dict = param.String()

    _template: str = """<div id="phighchart"></div>"""
    _scripts: dict = {}
    __javascript__ = ["https://code.highcharts.com/highcharts.js", "https://code.highcharts.com/modules/exporting.js"]
    prefix: str = "document.addEventListener('DOCContentLoaded', function() {"
    suffix: str = "});"
    
    def __init__(self, **params):
        super().__init__(**params)
        self._scripts = self._generate_highchart(self.chart_js)

    def _generate_highchart(self, chart_js: str):
        # remove the prefix and suffix
        chart_js_literal: str = chart_js[len(PanelHighchart.prefix):len(chart_js)-len(PanelHighchart.suffix)]
        
        # and replace null with the phighcharts div, which we can reference in the scripts as if the variable already exists
        chart_js_literal = chart_js_literal.replace("null,", "phighchart,")
        
        init: str = """console.log("Initing...");
if (phighchart !== null) {
    """ + chart_js_literal + """
} else {
    console.log("Not found...");
}"""
        render: str = f"""console.log("Rendering...");
self.init();"""
        scripts: dict = {
            "init": init,
            "render": render
        }
        return scripts

and then call with pnl_chart: PanelHighchart = PanelHighchart(chart_js=chart.to_js_literal()).

If we could optionally drop the JS event listener wrapper, that would solve having to remove it manually. The rest is just setting the HTML element to the element reference itself for highcharts to render to (and some debugging comments that can be removed!).

from highcharts-core.

hcpchris avatar hcpchris commented on May 23, 2024

Wow, @IanG-21 ! Thanks! This is super helpful, looks great, and I'll be taking a closer look at it shortly as I look at the integration with Panel. In the meantime, if you'd like to clean-up and file a PR, that would be awesome - but if not, then no worries: I'll be experimenting with this a bit in preparation for hopefully the next minor release.

from highcharts-core.

hcpchris avatar hcpchris commented on May 23, 2024

Just by way of FYI, PR #131 has added a flag to remove the event listener prefix. While it hasn't been released yet, that PR has been merged into the release branch for the next Highcharts for Core minor version (v.1.6, in branch rc-v.1.6).

from highcharts-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.