Echarts學習:Django快速整合Echarts
阿新 • • 發佈:2020-07-23
django快速使用echarts準備
1.線上定製下載echarts
https://echarts.apache.org/zh/builder.html
2.建立一個django專案或者在已有的專案
1.配置檔案中確保資料庫配置、static配置、與新增專案名到INSTALLED_APPS下。
2.配置靜態檔案目錄static,目錄下建立:css、img、js。
3.儲存echarts.min.js到js目錄下。
快速靜態測試
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <html檔案title>ECharts</title> <!-- 引入 echarts.js --> {% load static %} <script src="{% static '/js/echarts.min.js' %}"></script> </head> <body> <!-- 為ECharts準備一個具備大小(寬高)的Dom --> <div id="main" style="width: 600px;height:400px;"></div> <script type="text/javascript"> // 基於準備好的dom,初始化echarts例項 var myChart = echarts.init(document.getElementById('main')); // 指定圖表的配置項和資料 var option = { title: { text: 'ECharts 入門示例' }, tooltip: {}, legend: { data:['銷量'] }, xAxis: { data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"] }, yAxis: {}, series: [{ name: '銷量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }; // 使用剛指定的配置項和資料顯示圖表。 myChart.setOption(option); </script> </body> </html>
from django.urls import path from app.views import TestView urlpatterns = [ path('test/',TestView.as_view()), ]urls檔案
from django.shortcuts import render from rest_framework.views import View from rest_framework.response import Response class TestView(View): def dispatch(self, request, *args, **kwargs): """ 請求到來之後,都要執行dispatch方法,dispatch方法根據請求方式不同觸發 get/post/put等方法 注意:APIView中的dispatch方法有好多好多的功能 """ return super().dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): return render(request, "test.html") def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')View檔案
django獲取資料庫中的資料傳遞給echarts
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts</title> <!-- 引入 echarts.js --> {% load static %} <script src="{% static '/js/echarts.min.js' %}"></script> </head> <body> <div id="main" style="width: 600px;height:400px;"></div> <script type="text/javascript"> // 基於準備好的dom,初始化echarts例項 console.log(name) var myChart = echarts.init(document.getElementById('main')); // 指定圖表的配置項和資料 var option = { title: { text: 'ECharts 入門示例' }, tooltip: {}, legend: { data: ['銷量'] }, xAxis: { data: {{ name|safe }} }, yAxis: {}, series: [{ name: '銷量', type: 'bar', data:{{ data|safe }} }] }; // 使用剛指定的配置項和資料顯示圖表。 myChart.setOption(option); </script> </body> </html>html檔案
from django.urls import path from app.views import TestView1 urlpatterns = [ path('test1/',TestView1.as_view()), ]urls檔案
from django.shortcuts import render from rest_framework.views import APIView,View from rest_framework.response import Response from rest_framework.request import Request from django.http import JsonResponse from serail import ser import json class TestView(View): def dispatch(self, request, *args, **kwargs): """ 請求到來之後,都要執行dispatch方法,dispatch方法根據請求方式不同觸發 get/post/put等方法 注意:APIView中的dispatch方法有好多好多的功能 """ return super().dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): return render(request, "test.html") def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容') class TestView1(View): def dispatch(self, request, *args, **kwargs): """ 請求到來之後,都要執行dispatch方法,dispatch方法根據請求方式不同觸發 get/post/put等方法 注意:APIView中的dispatch方法有好多好多的功能 """ return super().dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): name = ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"] data = [56, 40, 54, 23, 12, 31] return render(request, "test1.html",{"name":name,"data":data}) def post(self, request, *args, **kwargs): return Response('POST請求,響應內容') def put(self, request, *args, **kwargs): return Response('PUT請求,響應內容')