Giter Club home page Giter Club logo

echarts's Introduction

echarts

echarts地图可视化模板-基于python

@yangcheng

import numpy as np
import json
import codecs

import echarts
from haversine import calculate_the_center_point
from coord_converse import wgs84togcj02, gcj02tobd09

def visualize(coordinate):
    output_file='echarts.html'
    nums = len(coordinate)
    names = ['p' + str(i) for i in range(nums)]
    colors = ['gold'] * nums
    symbols = ['circle'] * nums

    #coordinate = [[lat0, lng0], [lat1, lng1], ...]
    lats, lngs = np.array(zip(*coordinate))

    #WGS84 converse to BD09(intermediate: GCJ02)
    # lngs, lats = wgs84togcj02(lngs, lats)
    # lngs, lats = gcj02tobd09(lngs, lats)

    coordinate = np.vstack((lngs,lats)).T

    central_lng, central_lat = calculate_the_center_point(coordinate)

    startPoint = "{\nx: %s,\ny: %s\n}" % (central_lng, central_lat,)

    option_data = generate_option_data(names, colors, symbols)

    geoCoord = generate_geoCoord(names,coordinate)

    echarts_html = echarts.ECHARTS.format(startPoint=startPoint,option_data=option_data,geoCoord=geoCoord)

    echarts_html = echarts_html.replace(echarts.left,'{')
    echarts_html = echarts_html.replace(echarts.right,'}')

    output_file = output_file + '.html' if output_file.split('.')[-1] != 'html' else output_file

    with codecs.open(output_file, 'w', encoding='utf8') as html:
        html.write(echarts_html)
 
 
 def generate_option_data(names, colors, symbols):
    data = u"{name:'%s',value:{colorValue:'%s'},symbol:'%s'}"
    option_data = ''
    nums = len(names)
    for k,(name, color, symbol) in enumerate(zip(names, colors, symbols)):
        if k == nums-1:
            option_data += data % (name,color,symbol) + '\n'
        else:
            option_data += data % (name,color,symbol) + ',\n'
    
    return option_data

def generate_geoCoord(names,coordinate):
    if isinstance(coordinate, np.ndarray):
        coordinate = coordinate.tolist()
    geoCoord = dict(zip(names, coordinate))
    geoCoord = json.dumps(geoCoord, ensure_ascii=False, encoding='utf-8')
    geoCoord = geoCoord.replace('],', '],\n')
    return geoCoord
    
if __name__ == '__main__':
    coordinate = [
	[13.7384619517, 100.5609031209],
	[13.7455625134, 100.5626363308],
	[13.7565705087, 100.5459990912],
	[13.7612351636, 100.5261591264],
	[13.7576074396, 100.4965828831],
	[13.7512687035, 100.4922660627],
	[13.7452260638, 100.5236787536]
    ]
    visualize(coordinate)

echarts's People

Contributors

ivanzhangbo avatar wialsh avatar

Watchers

James Cloos avatar  avatar

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.