Vue项目中使用Highcharts

记录以下工作中遇到的问题

1. 项目安装Highcharts依赖

npm  install highcharts  --save

2. 组件内部引入需要的可视化图表:需要timeline就在modules下引入

这里很关键 之前项目中使用老是报错找不到依赖原来引入依赖有问题
import Highcharts from 'highcharts'//必须引入
import timeline from 'highcharts/modules/timeline';需要什么用什么modules下 如:timeline,area,funnel3d等等
timeline(Highcharts);///实例化

3. 组件内 完整示例

<template>
  <figure class="highcharts-figure">
    <div id="container"></div>
  </figure>
</template>
<style lang='scss'>
.highcharts-strong {
  font-weight: bold;
}

.highcharts-figure,
.highcharts-data-table table {
  min-width: 320px;
  max-width: 600px;
  margin: 1em auto;
}

.highcharts-data-table table {
  font-family: Verdana, sans-serif;
  border-collapse: collapse;
  border: 1px solid #ebebeb;
  margin: 10px auto;
  text-align: center;
  width: 100%;
  max-width: 500px;
}

.highcharts-data-table caption {
  padding: 1em 0;
  font-size: 1.2em;
  color: #555;
}

.highcharts-data-table th {
  font-weight: 600;
  padding: 0.5em;
}

.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
  padding: 0.5em;
}

.highcharts-data-table thead tr,
.highcharts-data-table tr:nth-child(even) {
  background: #f8f8f8;
}

.highcharts-data-table tr:hover {
  background: #f1f7ff;
}
</style>
<script>
// 导入chart组件
import Highcharts from "highcharts";
import timeline from "highcharts/modules/timeline"; 需要什么用什么modules/timeline
timeline(Highcharts);

export default {
  mounted: function () {
    this.HighchartsData();
  },
  methods: {
    HighchartsData() {
      /
      Highcharts.chart("container", {
        chart: {
          type: "timeline",
        },
        accessibility: {
          screenReaderSection: {
            beforeChartFormat:
              "<h5>{chartTitle}</h5>" +
              "<div>{typeDescription}</div>" +
              "<div>{chartSubtitle}</div>" +
              "<div>{chartLongdesc}</div>" +
              "<div>{viewTableButton}</div>",
          },
          point: {
            valueDescriptionFormat:
              "{index}. {point.label}. {point.description}.",
          },
        },
        xAxis: {
          visible: false,
        },
        yAxis: {
          visible: false,
        },
        title: {
          text: "Timeline of Space Exploration",
        },
        subtitle: {
          text: 'Info source: <a href="https://en.wikipedia.org/wiki/Timeline_of_space_exploration">www.wikipedia.org</a>',
        },
        colors: [
          "#4185F3",
          "#427CDD",
          "#406AB2",
          "#3E5A8E",
          "#3B4A68",
          "#363C46",
        ],
        series: [
          {
            data: [
              {
                name: "First dogs",
                label: "1951: First dogs in space",
                description:
                  "22 July 1951 First dogs in space (Dezik and Tsygan) ",
              },
              {
                name: "Sputnik 1",
                label: "1957: First artificial satellite",
                description:
                  "4 October 1957 First artificial satellite. First signals from space.",
              },
              {
                name: "First human spaceflight",
                label: "1961: First human spaceflight (Yuri Gagarin)",
                description:
                  "First human spaceflight (Yuri Gagarin), and the first human-crewed orbital flight",
              },
              {
                name: "First human on the Moon",
                label: "1969: First human on the Moon",
                description:
                  "First human on the Moon, and first space launch from a celestial body other than the Earth. First sample return from the Moon",
              },
              {
                name: "First space station",
                label: "1971: First space station",
                description:
                  "Salyut 1 was the first space station of any kind, launched into low Earth orbit by the Soviet Union on April 19, 1971.",
              },
              {
                name: "Apollo–Soyuz Test Project",
                label: "1975: First multinational manned mission",
                description:
                  "The mission included both joint and separate scientific experiments, and provided useful engineering experience for future joint US–Russian space flights, such as the Shuttle–Mir Program and the International Space Station.",
              },
            ],
          },
        ],
      });
      
    },
  },
};
</script>

4. 页面展示

 

具体使用什么图表可以参照文档修改

中文文档:兼容 IE6+、完美支持移动端、图表类型丰富的 HTML5 交互图表 | Highcharts

英文文档:https://www.highcharts.com/demo (不建议,实在是卡)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 可以与 Highcharts 集成,以下是一种使用 Highcharts 的方法: 首先,您需要在项目安装 Highcharts: ```bash npm install highcharts --save ``` 然后,在您的 Vue 组件,您可以按照以下步骤使用 Highcharts: 1. 导入 Highcharts 和相关模块: ```javascript import Highcharts from 'highcharts'; import HighchartsVue from 'highcharts-vue'; // 如果需要使用某些模块,可以导入对应的模块 import HCExporting from 'highcharts/modules/exporting'; import HCExportData from 'highcharts/modules/export-data'; // 在 Vue 注册 HighchartsVue 插件 Vue.use(HighchartsVue); // 如果需要使用某些模块,也可以在 Vue 注册对应的模块 HCExporting(Highcharts); HCExportData(Highcharts); ``` 2. 在模板使用 Highcharts: ```html <template> <div> <highcharts :options="chartOptions"></highcharts> </div> </template> ``` 3. 在组件定义 Highcharts 的配置项: ```javascript export default { data() { return { chartOptions: { // Highcharts 配置项 // 可以设置图表的类型、数据、样式等 }, }; }, }; ``` 通过以上步骤,您就可以在 Vue 3 使用 Highcharts 来创建图表了。根据 Highcharts 的文档,您可以根据需要配置不同的图表类型、数据和样式等。 请注意,以上代码只是一个简单示例,具体的配置和使用方法可以根据您的需求进行调整。您可以参考 Highcharts 的官方文档(https://www.highcharts.com/docs/index)了解更多关于配置和使用 Highcharts 的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值