Code Monkey home page Code Monkey logo

highcharts-maps's Introduction

Highcharts Maps for Python

High-end data and map visualizations for the Python ecosystem

Highcharts Maps for Python is an extension to the Highcharts Core for Python library, providing a Python wrapper for the Highcharts Maps JavaScript data visualization library.

Highcharts Maps for Python also supports

  • Highcharts Core (JS) - the core Highcharts data visualization library
  • The Highcharts Export Server - enabling the programmatic creation of static (downloadable) data visualizations

The library supports Highcharts (JS) v.10.2 and higher, including Highcharts (JS) v.11.4.0.

Highcharts Maps for Python is fully integrated with the broader Python ecosystem, offering native integrations with:

  • Jupyter Labs/Notebook. You can now produce high-end and interactive plots and renders using the full suite of Highcharts visualization capabilities.
  • Pandas. Automatically produce data visualizations from your Pandas dataframes
  • PySpark. Automatically produce data visualizations from data in a PySpark dataframe.
  • GeoPandas. Automatically incorporate GIS / map visualizations with data from your GeoPandas GeoDataFrames.
  • Topojson. Automatically visualizes TopoJSON map geometries.
  • Geojson. Automatically visualizes GeoJSON map geometries.

COMPLETE DOCUMENTATION: https://maps-docs.highchartspython.com/en/latest/index.html


The Highcharts for Python Toolkit

The Highcharts Maps for Python library is part of the broader Highcharts for Python Toolkit, which together provides comprehensive support across the entire Highcharts suite of data visualization libraries:

Python Library JavaScript Library Description
Highcharts Core for Python Highcharts Core (JS) (this library) the core Highcharts data visualization library
Highcharts Stock for Python Highcharts Stock (JS) the time series visualization extension to Highcharts Core
Highcharts Maps for Python Highcharts Maps (JS) the map visualization extension to Highcharts Core
Highcharts Gantt for Python Highcharts Gantt (JS) the Gantt charting extension to Highcharts Core
(all libraries in the Python toolkit) The Highcharts Export Server enabling the programmatic creation of static (downloadable) data visualizations

Installation

To install Highcharts Maps for Python, just execute:

$ pip install highcharts-maps

Before you install, please be aware of the following "hard" dependencies:

You can find more information about soft and development dependencies in the complete documentation.


Why Highcharts for Python?

Highcharts is the world's most popular, most powerful, category-defining JavaScript data visualization library. If you are building a web or mobile app/dashboard that will be visualizing data in some fashion, you should absolutely take a look at the Highcharts suite of solutions. Take a peak at some fantastic demo visualizations.

As a suite of JavaScript libraries, Highcharts is written in JavaScript, and is used to configure and render data visualizations in a web browser (or other JavaScript-executing) environment. As a set of JavaScript libraries, its audience is JavaScript developers. But what about the broader ecosystem of Python developers and data scientists?

Given Python's increasing adoption as the technology of choice for data science and for the backends of leading enterprise-grade applications, Python is often the backend that delivers data and content to the front-end...which then renders it using JavaScript and HTML.

There are numerous Python frameworks (Django, Flask, Tornado, etc.) with specific capabilities to simplify integration with Javascript frontend frameworks (React, Angular, VueJS, etc.). But facilitating that with Highcharts has historically been very difficult. Part of this difficulty is because the Highcharts JavaScript suite - while supporting JSON as a serialization/deserialization format - leverages JavaScript object literals to expose the full power and interactivity of its data visualizations. And while it's easy to serialize JSON from Python, serializing and deserializing to/from JavaScript object literal notation is much more complicated.

This means that Python developers looking to integrate with Highcharts typically had to either invest a lot of effort, or were only able to leverage a small portion of Highcharts' rich functionality.

So we wrote the Highcharts for Python Toolkit to bridge that gap.

Highcharts for Python provides Python object representation for all of the JavaScript objects defined in the Highcharts (JavaScript) API. It provides automatic data validation, and exposes simple and standardized methods for serializing those Python objects back-and-forth to JavaScript object literal notation.

Highcharts Maps for Python in particular provides support for the Highcharts Maps extension, which is designed to provide extensive map and data visualization capabilities optimized for GIS (Geographic Information System) data visualization, with robust interactivity. For ease of use, it also includes the full functionality of Highcharts Core for Python as well.

Key Highcharts Maps for Python Features

  • Clean and consistent API. No reliance on "hacky" code, dict and JSON serialization, or impossible to maintain / copy-pasted "spaghetti code".
  • Comprehensive Highcharts support. Every single Highcharts chart type and every single configuration option is supported in Highcharts Maps for Python. This includes the over 70 data visualization types supported by Highcharts Core and the four core map visualizations available in Highcharts Maps.

    Every Highcharts for Python library provides full support for the rich JavaScript formatter (JS callback functions) capabilities that are often needed to get the most out of Highcharts' visualization and interaction capabilities.

    Note

    See also:

  • Simple JavaScript Code Generation. With one method call, produce production-ready JavaScript code to render your interactive visualizations using Highcharts' rich capabilities.
  • Easy Chart Download. With one method call, produce high-end static visualizations that can be downloaded or shared as files with your audience. Produce static charts using the Highsoft-provided Highcharts Export Server, or using your own private export server as needed.
  • Asynchronous Map Data Retrieval. To minimize the amount of data transferred over the wire, Highcharts Maps for Python has built-in support for the configuration of asynchronous client-side retrieval of your map data.
  • Automatic TopoJSON Optimization. To minimize the amount of data transferred over the wire, Highcharts Maps for Python automatically converts your map geometries to highly-efficient TopoJSON topologies while still allowing you to work with GeoJSON data if you choose to.
  • Integration with GeoPandas, Pandas, and PySpark. With two lines of code, produce a high-end interactive visualization of your GeoPandas, Pandas, or PySpark dataframes.
  • Consistent code style. For Python developers, switching between Pythonic code conventions and JavaScript code conventions can be...annoying. So the Highcharts for Python toolkit applies Pythonic syntax with automatic conversion between Pythonic snake_case notation and JavaScript camelCase styles.

Highcharts Maps for Python vs Alternatives

For a discussion of Highcharts Maps for Python in comparison to alternatives, please see the COMPLETE DOCUMENTATION: https://maps-docs.highchartspython.com/en/latest/index.html


Hello World, and Basic Usage

1. Import Highcharts Maps for Python

# PRECISE IMPORT PATTERN  
# This method of importing Highcharts Maps for Python objects yields the fastest
# performance for the import statement. However, it is more verbose and requires
# you to navigate the extensive Highcharts Maps for Python API.

# Import classes using precise module indications. For example:
from highcharts_maps.chart import Chart
from highcharts_maps.global_options.shared_options import SharedMapsOptions
from highcharts_maps.options import HighchartsMapsOptions
from highcharts_maps.options.plot_options.map import MapOptions
from highcharts_maps.options.series.map import MapSeries

# CATCH-ALL IMPORT PATTERN
# This method of importing Highcharts Maps for Python classes has relatively slow
# performance because it imports hundreds of different classes from across the entire
# library. This is also a known anti-pattern, as it obscures the namespace within the
# library. Both may be acceptable to you in your use-case, but do use at your own risk.

# Import objects from the catch-all ".highcharts" module.
from highcharts_maps import highcharts

# You can now access specific classes without individual import statements.
highcharts.Chart
highcharts.SharedMapsOptions
highcharts.HighchartsMapsOptions
highcharts.MapOptions
highcharts.MapSeries

2. Create Your Chart

# from a primitive array, using keyword arguments
my_chart = Chart(data = [[1, 23], [2, 34], [3, 45]], 
                 series_type = 'line')

# from a primitive array, using the .from_array() method
my_chart = Chart.from_array([[1, 23], [2, 34], [3, 45]], 
                            series_type = 'line')

# from a Numpy ndarray, using keyword arguments
my_chart = Chart(data = numpy_array, series_type = 'line')

# from a Numpy ndarray, using the .from_array() method
my_chart = Chart.from_array(data = numpy_array, series_type = 'line')

# from a JavaScript file
my_chart = Chart.from_js_literal('my_js_literal.js')

# from a JSON file
my_chart = Chart.from_json('my_json.json')

# from a Python dict
my_chart = Chart.from_dict(my_dict_obj)

# from a Pandas dataframe
my_chart = Chart.from_pandas(df)

# from a PySpark dataframe
my_chart = Chart.from_pyspark(df,
                              property_map = {
                                  'x': 'transactionDate',
                                  'y': 'invoiceAmt',
                                  'id': 'id'
                              },
                              series_type = 'line')

# from a CSV
my_chart = Chart.from_csv('/some_file_location/filename.csv')

# from a HighchartsOptions configuration object
my_chart = Chart.from_options(my_options)

# from a Series configuration, using keyword arguments
my_chart = Chart(series = my_series)

# from a Series configuration, using .from_series()
my_chart = Chart.from_series(my_series)

3. Configure Global Settings (optional)

# Import SharedMapsOptions
from highcharts_maps.global_options.shared_options import SharedMapsOptions

# from a JavaScript file
my_global_settings = SharedMapsOptions.from_js_literal('my_js_literal.js')

# from a JSON file
my_global_settings = SharedMapsOptions.from_json('my_json.json')

# from a Python dict
my_global_settings = SharedMapsOptions.from_dict(my_dict_obj)

# from a HighchartsOptions configuration object
my_global_settings = SharedMapsOptions.from_options(my_options)

4. Configure Your Chart / Global Settings

from highcharts_core.options.title import Title
from highcharts_core.options.credits import Credits

# EXAMPLE 1.
# Using dicts
my_chart.title = {
    'align': 'center',
    'floating': True,
    'text': 'The Title for My Chart',
    'use_html': False,
}

my_chart.credits = {
    'enabled': True,
    'href': 'https://www.highchartspython.com/',
    'position': {
        'align': 'center',
        'vertical_align': 'bottom',
        'x': 123,
        'y': 456
    },
    'style': {
        'color': '#cccccc',
        'cursor': 'pointer',
        'font_size': '9px'
    },
    'text': 'Chris Modzelewski'
}

# EXAMPLE 2.
# Using direct objects
from highcharts_core.options.title import Title
from highcharts_core.options.credits import Credits

my_title = Title(text = 'The Title for My Chart',
                 floating = True, 
                 align = 'center')
my_chart.options.title = my_title

my_credits = Credits(text = 'Chris Modzelewski', 
                     enabled = True, 
                     href = 'https://www.highchartspython.com')
my_chart.options.credits = my_credits

5. Generate the JavaScript Code for Your Chart

Now having configured your chart in full, you can easily generate the JavaScript code that will render the chart wherever it is you want it to go:

# as a string
js_as_str = my_chart.to_js_literal()

# to a file (and as a string)
js_as_str = my_chart.to_js_literal(filename = 'my_target_file.js')

6. Generate the JavaScript Code for Your Global Settings (optional)

# as a string
global_settings_js = my_global_settings.to_js_literal()

# to a file (and as a string)
global_settings_js = my_global_settings.to_js_literal('my_target_file.js')

7. Generate a Static Version of Your Chart

# as in-memory bytes
my_image_bytes = my_chart.download_chart(format = 'png')

# to an image file (and as in-memory bytes)
my_image_bytes = my_chart.download_chart(filename = 'my_target_file.png',
                                         format = 'png')

8. Render Your Chart in a Jupyter Notebook

my_chart.display()

Getting Help/Support

The Highcharts for Python Toolkit comes with all of the great support that you are used to from working with the Highcharts JavaScript libraries. When you license the toolkit, you are welcome to use any of the following channels to get help using the toolkit:

FOR MORE INFORMATION: https://www.highchartspython.com/get-help


Contributing

We welcome contributions and pull requests! For more information, please see the Contributor Guide. And thanks to all those who've already contributed!


Testing

We use TravisCI for our build automation and ReadTheDocs for our documentation.

Detailed information about our test suite and how to run tests locally can be found in our Testing Reference.

highcharts-maps's People

Contributors

hcpchris avatar insightindustry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

highcharts-maps's Issues

Explicit Output of HC Defaults

In v.1.0.0, Highcharts Maps for Python outputs the JavaScript literal object where any unspecified property is serialized to a dict as None and subsequently stripped from the serialized JS object literal version. Logically, this will then apply the Highcharts default value for that property.

In v.1.x, it would be nice (to aid in debugging / diagnostics) to have an show_default_values argument in the .to_js_literal() method which if True will output the JS literal with those default values explicitly displayed, rather than stripped. By default, this argument should be False.

Needed <script/> tags only

In v.1.0.0, Highcharts Maps for Python will generate the full set of <script/> tags that download all of the Highcharts JS modules - whether the visualization uses that module or not.

In v.1.x, it would be good for Highcharts Maps for Python to:

  • generate the set of <script/> tags that are strictly necessary for the visualization - if a module is not needed, then it should not be included
  • expose a public method for retrieving those <script/> tags for inclusion in a <head/> location
  • expose a public method for dynamically retrieving those <script/> tags and appending them to the <head/> location based on Javascript Promises (as is used for the Jupyter Labs rendering in v.1.0.0).

Support for Styled Mode Configuration

In v.1.0.0 of Highcharts Maps for Python supports the enabling/disabling of styled mode using the appropriate options. However, any configuration of the CSS has to occur outside of the library itself.

In v.1.x (possibly v.2.0 due to the complexity of this enhancement) it would be helpful if the library also provided for the configuration of the CSS styles that are applicable if using Styled Mode.

Bug: Map Fails to Render with Data Loaded via GeoPandas

As documented in this StackOverFlow question, the user is reporting that:

from highcharts_maps.chart import Chart
import geopandas as gpd
import numpy as np

df = gpd.read_file('https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements-version-simplifiee.geojson')
df['value'] = np.random.randint(0,100,df.shape[0])
my_chart = Chart.from_geopandas(df, property_map = {
                                         'id': 'code',
                                          'name':'nom',
                                         'value': 'value'
                                     }, series_type = 'map')
my_chart.display()

causes several problems:

  1. The chart does not display.
  2. It would seem (per the reporter) that my_chart.options does not contain any of the geometry data extracted from the relevant GeoJSON.

Furthermore, the following:

my_map_data = MapData.from_geojson('https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements-version-simplifiee.geojson')
my_chart.set_map_data(my_map_data)

fails to properly configure map data.

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.