示例#1
0
def plot(
        series, options=dict(), type='line', name=False,
        height=400, save=False, stock=False, show='tab', display=True):
    """
    Make a highchart plot with all data embedded in the HTML
    :param type: Type of the chart (will overwrite options['chart']['type'] if specified).
    :param series: The necessary data, can be a list of dictionaries or a dataframe
    :param options: Options for the chart
    :param height: Chart height
    :param save: Specify a filename to save the HTML file if wanted.
    :param stock: Set to False to use Highcharts instead of highstock
    :param show: Determines how the chart is shown. Can be one of the following options:
        - 'tab': Show the chart in a new tab of the default browser
        - 'window': Show the chart in a new window of the default browser
        - 'inline': Show the chart inline (only works in IPython notebook)
    :param display: A list containing the keys of the variables you want to show initially in the plot
    :return:
    """
    try:
        if options['chart']:
            options['chart'].update(dict(type=type))
    except KeyError:
        options['chart'] = dict(type=type)

    # Convert to a legitimate series object
    series = to_series(series, name)

    # Set the display option
    series = set_display(series, display)

    with open(os.path.join(package_directory, "index.html"), "r") as html:
        inline = MyTemplate(html.read()).substitute(
            path=package_directory,
            series=json.dumps(series, cls=ChartsJSONEncoder),
            options=json.dumps(options),
            highstock=json.dumps(stock),
            height=str(height) + "px"
        )

    if save:
        with open(save, "w") as text_file:
            text_file.write(inline)
    else:
        if show != 'inline':
            save = 'index.html'
            with open(save, "w") as text_file:
                text_file.write(inline)

    return show_plot(inline, save, show)
示例#2
0
def plot(series, options=dict(), **kwargs):
    """
    Make a highchart plot with all data embedded in the HTML
    :param type: Type of the chart (will overwrite options['chart']['type'] if specified).
    :param series: The necessary data, can be a list of dictionaries or a dataframe
    :param options: Options for the chart. This can one of the following:
        - A Dictionary
        - The path to a json file
        - A json string
    :param height: Chart height
    :param save: Specify a filename to save the HTML file if wanted.
    :param stock: Set to False to use Highcharts instead of highstock
    :param show: Determines how the chart is shown. Can be one of the following options:
        - 'tab': Show the chart in a new tab of the default browser
        - 'window': Show the chart in a new window of the default browser
        - 'inline': Show the chart inline (only works in IPython notebook)
    :param display: A list containing the keys of the variables you want to show initially in the plot
    :return: The chart to display
    """

    # Check if options is a json string or file
    if isinstance(options, str):
        if '.json' in options:
            options = load_options(options)
        else:
            try:
                options = json.loads(options)
            except ValueError:
                raise ValueError('Your options string is not valid JSON!')

    chart_settings = default_settings.copy()
    chart_options = default_options.copy()

    chart_settings.update(kwargs)
    chart_options.update(options)

    keys = chart_settings.keys()

    for key in keys:
        if key not in ['options', 'name', 'display', 'save', 'show', 'height', 'type', 'stock', 'width']:
            raise AttributeError(key + ' is not a valid option!')

    options = chart_options
    name = chart_settings['name']
    display = chart_settings['display']
    save = chart_settings['save']
    show = chart_settings['show']
    type = chart_settings['type']
    stock = chart_settings['stock']

    try:
        if options['chart']:
            options['chart'].update(dict(type=type))
    except KeyError:
        options['chart'] = dict(type=type)

    try:
        if not options['height']:
            options['chart'].update(dict(type=type))
    except KeyError:
        options['chart'] = dict(type=type)

    # Convert to a legitimate series object
    series = to_series(series, name)

    # Set the display option
    series = set_display(series, display)

    # Get the save extension
    if save:
        extension = os.path.splitext(save)[1]
    else:
        extension = False

    saveSVG = False
    saveHTML = False

    if extension == '.svg':
        saveSVG = save
    if show != 'inline':
        saveHTML = 'index.html'
    if extension == '.html':
        saveHTML = save

    if 'settingsFile' in options:
        settings_file = options['settingsFile'][:-5]
    else:
        settings_file = 'settings'

    with open(os.path.join(package_directory, "index.html"), "r") as html:
        html = MyTemplate(html.read()).substitute(
            path=package_directory,
            series=json.dumps(series, cls=ChartsJSONEncoder),
            options=remove_quotes(options),
            highstock=json.dumps(stock),
            url=json.dumps(address),
            save=json.dumps(saveSVG),
            settingsFile=json.dumps(settings_file)
        )

    if saveHTML:
        with open(saveHTML, "w") as text_file:
            text_file.write(html + TABDEPS)

    return show_plot(html, saveHTML, show)
示例#3
0
def plot(series, options=dict(), **kwargs):
    """
    Make a highchart plot with all data embedded in the HTML
    :param type: Type of the chart (will overwrite options['chart']['type'] if specified).
    :param series: The necessary data, can be a list of dictionaries or a dataframe
    :param options: Options for the chart. This can one of the following:
        - A Dictionary
        - The path to a json file
        - A json string
    :param height: Chart height
    :param save: Specify a filename to save the HTML file if wanted.
    :param stock: Set to False to use Highcharts instead of highstock
    :param show: Determines how the chart is shown. Can be one of the following options:
        - 'tab': Show the chart in a new tab of the default browser
        - 'window': Show the chart in a new window of the default browser
        - 'inline': Show the chart inline (only works in IPython notebook)
    :param display: A list containing the keys of the variables you want to show initially in the plot
    :return: The chart to display
    """

    # Check if options is a json string or file
    if isinstance(options, str):
        if '.json' in options:
            options = load_options(options)
        else:
            try:
                options = json.loads(options)
            except ValueError:
                raise ValueError('Your options string is not valid JSON!')

    chart_settings = default_settings.copy()
    chart_options = default_options.copy()

    chart_settings.update(kwargs)
    chart_options.update(options)

    keys = chart_settings.keys()

    for key in keys:
        if key not in [
                'options', 'name', 'display', 'save', 'show', 'height', 'type',
                'stock', 'width'
        ]:
            raise AttributeError(key + ' is not a valid option!')

    options = chart_options
    name = chart_settings['name']
    display = chart_settings['display']
    save = chart_settings['save']
    show = chart_settings['show']
    type = chart_settings['type']
    stock = chart_settings['stock']

    try:
        if options['chart']:
            options['chart'].update(dict(type=type))
    except KeyError:
        options['chart'] = dict(type=type)

    try:
        if not options['height']:
            options['chart'].update(dict(type=type))
    except KeyError:
        options['chart'] = dict(type=type)

    # Convert to a legitimate series object
    series = to_series(series, name)

    # Set the display option
    series = set_display(series, display)

    # Get the save extension
    if save:
        extension = os.path.splitext(save)[1]
    else:
        extension = False

    saveSVG = False
    saveHTML = False

    if extension == '.svg':
        saveSVG = save
    if show != 'inline':
        saveHTML = 'index.html'
    if extension == '.html':
        saveHTML = save

    if 'settingsFile' in options:
        settings_file = options['settingsFile'][:-5]
    else:
        settings_file = 'settings'

    with open(os.path.join(package_directory, "index.html"), "r") as html:
        html = MyTemplate(html.read()).substitute(
            path=package_directory,
            series=json.dumps(series, cls=ChartsJSONEncoder),
            options=remove_quotes(options),
            highstock=json.dumps(stock),
            url=json.dumps(address),
            save=json.dumps(saveSVG),
            settingsFile=json.dumps(settings_file))

    if saveHTML:
        with open(saveHTML, "w") as text_file:
            text_file.write(html + TABDEPS)

    return show_plot(html, saveHTML, show)
 def show(self):
     return show_plot(self.inline, self.html, self.show_property, async=self.path)