首页 > 其他分享 >ZingChart绘制金融仪表板

ZingChart绘制金融仪表板

时间:2024-05-23 10:42:45浏览次数:17  
标签:ZingChart wdata height width rate let document 绘制 仪表板

  • 代码案例
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>ZingChart Demo: Financial Widget Dashboard</title>
  <script nonce="undefined" src="./zingchart.min.js"></script>
  <style>
    .zc-body {
      background: #fff;
    }

    #myChart {
      margin: 10px 0 20px;
      opacity: 0;
      visibility: hidden;
      transition: opacity .3s;
    }

    #myChart.loaded {
      opacity: 1;
      visibility: visible;
    }

    .zc-demo {
      margin: 0 auto;
      width: 100%;
      max-width: 910px;
    }

    .zc-favorites {
      z-index: 100;
      position: absolute;
      top: 0;
      left: 0;
      box-shadow: 3px 3px 3px #333;
    }

    .zc-favorites-wrap {
      margin-top: 10px;
      margin: 0 auto;
      padding: 10px;
      min-height: 120px;
      background: #ebebeb;
      border: 0;
    }

    .zc-favorites-headline {
      font-size: 13px;
      display: block;
      margin-bottom: 7px;
    }

    .zc-favorites-db {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-column-gap: 10px;
      grid-row-gap: 10px;
    }

    .zc-favorites-fb-db {
      float: left;
    }

    .zc-ref {
      display: none;
    }
  </style>
</head>

<body class="zc-body">

  <div class="zc-demo">

    <div id="myChart">
      <a href="https://www.zingchart.com/" rel="noopener" class="zc-ref">Powered by ZingChart</a>
    </div>

    <!-- <div class="zc-favorites-wrap">
      <strong class="zc-favorites-headline">Favorites (drag from above)</strong>
      <div id="db" class="zc-favorites-db"></div>
    </div> -->

  </div>

  <script>
    ZC.LICENSE = ["xxx", "xxx"]; // Define Module Location
    zingchart.MODULESDIR = 'https://cdn.zingchart.com/modules/';

    // DEFINE DATA
    // -----------------------------
    // Data config
    let chartData = {
      type: 'null',
      backgroundColor: '#ddd #eee',
      height: '100%',
      width: '100%',
      widgets: [{             // 设置小部件的样式
          type: 'exchange',
          rate: 'EUR/USD',
          colors: [
            ['#55154D', '#4E0E46'], '#fff', '#ccc'
          ],
          refresh: 500,
          ticks: 100,
          width: 450,
          x: '10px',
          y: '10px',
        },
        {
          type: 'exchange',       // 定义数据部件,并设置样式
          rate: 'USD/CAD',
          colors: [
            ['#56000E', '#5F0017'], '#fff', '#ccc'
          ],
          x: '470px',
          y: '10px',
        },
        {
          type: 'exchange',
          rate: 'USD/JPY',
          colors: [
            ['#014358', '#013D52'], '#fff', '#ccc'
          ],
          decimals: 2,
          height: 200,
          width: 200,
          x: '700px',
          y: '10px',
        },
        {
          type: 'exchange',
          rate: 'BTC/USD',
          colors: [
            ['#264031', '#243C2E'], '#fff', '#ccc'
          ],
          decimals: 2,
          refresh: 200,
          ticks: 60,
          x: '10px',
          y: '160px',
        },
        {
          type: 'exchange',
          rate: 'CRUDE OIL WTI',
          colors: [
            ['#264F14', '#244B13'], '#fff', '#ccc'
          ],
          decimals: 2,
          x: '240px',
          y: '160px',
        },
        {
          type: 'exchange',
          rate: 'GOLD',
          colors: [
            ['#704F00', '#6B4A00'], '#fff', '#ccc'
          ],
          decimals: 2,
          height: 260,
          refresh: 2000,
          x: '470px',
          y: '160px',
        },
        {
          type: 'exchange',
          rate: 'GBP/USD',
          colors: [
            ['#262400', '#242200'], '#fff', '#ccc'
          ],
          height: 110,
          x: '10px',
          y: '310px',
        },
        {
          type: 'exchange',
          rate: 'NZD/USD',
          colors: [
            ['#260031', '#24002E'], '#fff', '#ccc'
          ],
          height: 110,
          x: '240px',
          y: '310px',
        },
        {
          type: 'exchange',
          rate: 'USD/CHF',
          colors: [
            ['#363636', '#333333'], '#fff', '#ccc'
          ],
          height: 200,
          width: 200,
          x: '700px',
          y: '220px',
        },
      ],
    };

    // 包含用于渲染图表的数据
    let chartDataFB = {
      backgroundColor: '#ddd #eee',
      type: 'null',
      widgets: [],
    };
    let SEQ = 0;

    // Main chart render location
    let chartId = 'myChart';

    // Favorites
    let fbId = 'fb';

    // INIT
    // -----------------------------
    // Append Amazon Script
    let aws = document.createElement('script');
    aws.src = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/374756/widgets.js';
    document.body.appendChild(aws);
    aws.addEventListener('load', init);

    //  模拟获取外部数据,并返回一个 JSON 对象
    // Constantly fetch new data sets
    window.fetchData = function(rate, callback) {
      let oRefValues = {
        'EUR/USD': 1.1143,
        'USD/JPY': 120.88,
        'USD/CAD': 1.2219,
        'BTC/USD': 232.71,
        'CRUDE OIL WTI': 58.59,
        GOLD: 1208.21,
        'GBP/USD': 1.552,
        'NZD/USD': 0.7368,
        'USD/CHF': 0.9364,
      };
      let fRefValue = oRefValues[rate],
        fDiff = fRefValue / 100;
      oRefValues[rate] = fRefValue + (-fDiff / 2 + fDiff * Math.random());
      let json = {
        rate: rate,
        ts: 100 * Math.round(new Date().getTime() / 100),
        value: oRefValues[rate],
      };
      callback.call(window, JSON.stringify(json));
    };

    // 初始化
    // LOAD EVENTS
    // -----------------------------
    // Run these after AWS script is init
    function init() {
      // Render the main demo
      zingchart.render({
        id: chartId,
        width: '100%',
        height: 430,
        output: 'svg',
        strictmode: true,
        data: chartData,        // 上面的小部件配置
        events: {
          load: loaded,
        },
      });

      // Bind 'mousedown' action when you click/drag a chart
      zingchart.bind(chartId, 'mousedown', md);
    }

    // CHART RENDERED
    // -----------------------------
    // Set CSS class to show chart after render
    function loaded() {
      let chart = document.querySelector('#' + chartId);
      chart.classList.add('loaded');
    }

    // 判断鼠标事件是否是鼠标左键的点击事件
    // DEFINE 'MOUSE DOWN' FUNCTION
    // -----------------------------
    function md(p) {
      // Early Exit: Element is not a button
      if (p.ev.button !== 0) return;
      // Add data
      let wdata = getData(p, chartData);
      // Early Exit: No data found
      if (!wdata) return;

      // Define data for 2nd location
      chartDataFB.widgets = [wdata];
      p.ev.preventDefault();

      // Add data to Favorites
      let appendData =
        '<div id="fb" class="zc-favorites" style="width:' +
        wdata.width +
        'px;height:' +
        wdata.height +
        'px;"></div>';
      document.body.insertAdjacentHTML('beforeend', appendData);

      // 定时任务
      setTimeout(function() {
        // Otherwise, create mouse functions and apply to the chart
        // ---
        // Get the Favorites DOM element
        let fb = document.querySelector('#' + fbId);
        // Early Exit: Not found
        if (!fb) return;

        // Position element
        fb.style.top = p.ev.clientY - wdata.height / 2 + 'px';
        fb.style.left = p.ev.clientX - wdata.width / 2 + 'px';

        // Bind mouse movements
        document.addEventListener('mousemove', mouseMove);
        document.addEventListener('mouseup', mouseUp);

        // Render 2nd chart in 'Favorites' DOM element
        zingchart.render({
          id: fbId,
          width: wdata.width,
          height: wdata.height,
          output: 'svg',
          strictmode: true,
          data: chartDataFB,
        });
      }, 0);

      // MOUSE FNS

      // Mousemove
      function mouseMove(ev) {      // 鼠标移动时调用
        ev.preventDefault();
        let fb = document.querySelector('#' + fbId);
        let doc = document.documentElement;
        let body = document.body;
        let offsetTop = doc.scrollTop ? doc.scrollTop : document.body.scrollTop;
        fb.style.top = ev.clientY - wdata.height / 2 + offsetTop + 'px';
        fb.style.left = ev.clientX - wdata.width / 2 + 'px';
      }

      // Mouseup
      function mouseUp(ev) {      // 鼠标释放时调用
        let fb = document.querySelector('#' + fbId);
        if (fb) {
          let db = document.querySelector('#db');
          let dbData = '<div id="fb' + SEQ + '" class="zc-favorites-fb-db"></div>';
          db.insertAdjacentHTML('beforeend', dbData);
          zingchart.render({
            id: fbId + SEQ,
            width: wdata.width,
            height: wdata.height,
            output: 'svg',
            strictmode: true,
            data: chartDataFB,
          });
          SEQ++;
          fb.remove();
          zingchart.exec(fbId, 'destroy');
        }
        document.removeEventListener('mousemove', mouseMove);
        document.removeEventListener('mouseup', mouseUp);
      }
    }

    // DEFINE 'GET DATA' FUNCTION
    // -----------------------------
    // Get the data from the clicked item
    function getData(p, data) {       // 传入参数,提取小部件的数据
      if (!data) return;
      let wdata;
      for (let w = 0; w < data.widgets.length; w++) {
        let rate = data.widgets[w].rate;
        let id = p.id + '-graph-' + p.id + rate.replace(/[^a-zA-Z0-9]/g, '');
        if (id === p.graphid) {
          wdata = data.widgets[w];
          wdata.x = 0;
          wdata.y = 0;
          wdata.width = 220;
          wdata.height = 100;
        }
      }
      return wdata;
    }
  </script>
</body>

</html>
  • 效果图

标签:ZingChart,wdata,height,width,rate,let,document,绘制,仪表板
From: https://www.cnblogs.com/dogleftover/p/18207865

相关文章

  • ZingChart绘制透明样条面积图
    代码案例,参考<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>ZingSoftDemo</title><scriptnonce="undefined"src="https://cdn.zingchart.com/zingchart.min.js"></scrip......
  • 可视化学习:使用极坐标参数方程和SDF绘制有趣的图案
    前言本文将介绍如何使用极坐标参数方程和上一篇文章提到的距离场SDF来绘制有趣的图案。说到曲线和几何图形的绘制,我们知道图形系统默认支持的是通过直角坐标绘制,但是有些曲线呢,不太容易使用直角坐标系来表示,却可以很方便地使用极坐标来表示,这个时候我们可以选择通过极坐标和直角......
  • ZingChart折线图
    代码案例,参考<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>ZingSoftDemo</title><scriptnonce="undefined"src="https://cdn.zingchart.com/zingchart.min.js"></scrip......
  • qt之点的绘制示例demo
    #include"mainwindow.h"#include"ui_mainwindow.h"#include<QPainter>#include<QColor>QColorm_color;intm_x=0;intm_y=0;intm_w=0;intm_h=0;MainWindow::MainWindow(QWidget*parent):QMainWindow(parent)......
  • 跨端自渲染绘制的实践与探索
    在过去的大半年中,我一直投身于一个跨端自渲染项目的研发工作中,负责其中的渲染模块。现在通过这篇文章,我想记录并分享我们在这个项目中的经验和挑战,希望能为大家日常开发中的涉及到渲染相关的工作带来一些启发和帮助。跨端自渲染的初衷跨端自渲染项目的愿景在于构建一个后端渲......
  • 用CSS3绘制iPhone手机
    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解`用CSS3绘制iPhone手机日期:2017-7-3阿珏css浏览:1825次评论:1条先上效果图,先睹为快。(这绝对不是一张图片。恩~这话怎么怪怪的~)......
  • d3d12龙书阅读----绘制几何体(上) 课后习题
    d3d12龙书阅读----绘制几何体(上)课后习题练习1完成相应的顶点结构体的输入-布局对象typedefstructD3D12_INPUT_ELEMENT_DESC{一个特定字符串将顶点结构体数组里面的顶点映射到顶点着色器的输入签名LPCSTRSemanticName;语义索引如果语义名相同的......
  • Python 潮流周刊#51:用 Python 绘制美观的图表
    本周刊由Python猫出品,精心筛选国内外的250+信息源,为你挑选最值得分享的文章、教程、开源项目、软件工具、播客和视频、热门话题等内容。愿景:帮助所有读者精进Python技术,并增长职业和副业的收入。本期周刊分享了12篇文章,12个开源项目,赠书5本《图解IT基础设施》,全文220......
  • 如何使用Python和Plotly绘制3D图形
    本文分享自华为云社区《Plotly绘制3D图形》,作者:柠檬味拥抱。在数据可视化领域,三维图形是一种强大的工具,可以展示数据之间的复杂关系和结构。Python语言拥有丰富的数据可视化库,其中Plotly是一款流行的工具,提供了绘制高质量三维图形的功能。本文将介绍如何使用Python和Plotly来绘......
  • 使用joinjs绘制流程图(十)-实战-绘制流程图+自定义节点样式(优化)
    问题前面自定义节点的样式坐标位置不对,我们希望自定义节点的坐标和rect元素的位置是一样的效果图代码<template><divclass="app"><divref="myholder"id="paper"></div></div></template><script>import*asjointfrom&......