如何在网页中显示数据图表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在网页中显示数据图表相关的知识,希望对你有一定的参考价值。

一、下载所需要的echarts.min.js文件
官网下载链接
csdn下载链接
二、引入 ECharts
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 引入 ECharts 文件 -->
<script src="echarts.min.js"></script>
</head>
</html>12345678

三、绘制一个简单的柱状图表
在绘图前我们需要为 ECharts 准备一个具备高宽的 DOM 容器。
然后就可以通过 echarts.init 方法初始化一个 echarts 实例并通过 setOption 方法生成一个简单的柱状图,下面是完整代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
<!-- 引入 echarts.js -->
<script src="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>12345678910111213141516171819202122232425262728293031323334353637383940
参考技术A 如何在网页

如何使用图表库分隔 JSON 数据以唯一显示?

【中文标题】如何使用图表库分隔 JSON 数据以唯一显示?【英文标题】:How to Seperate JSON data to display uniquely using Charts Library? 【发布时间】:2017-07-06 09:03:29 【问题描述】:

我能够显示 JSON 数据以使用图表库显示折线图。我已显示所有标记和主题,但我试图在 TermWise 中显示。如何使用 TermID 分隔 JSON 数据?

import UIKit
import Charts
import Alamofire
import SwiftyJSON


class ViewController: UIViewController , ChartViewDelegate


    @IBOutlet weak var text: UILabel!
    @IBOutlet weak var lineChartView: LineChartView!
    @IBOutlet weak var menu: UIBarButtonItem!
    var login_details : Login?
    var markdetail : [Marks] = []
    var markd : Marks?
    var myResponse  :   JSON    =   nil
//    var Subjects: [String] = []
    var subjectss : [String] = []
    var marks01 : [Double] = []
    var setname = String()

    let Subjects = ["eng" , "sci", "math", "soc"]
    let marks1 = [45,55,70,80]

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        menu.target = self.revealViewController()

        menu.action = #selector(SWRevealViewController.revealToggle(_:))

        if self.revealViewController() != nil 

            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
            self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
        

//        text.text = markd?.NAME

//        forlineView()
        getmarks()



    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    
    func getmarks()
        let student_id = login_details?.masterid
        let std_id_String = student_id?.replacingOccurrences(of: "[^0-9 ]", with: "", options: NSString.CompareOptions.regularExpression, range:nil)
        print("numberic",std_id_String!)

        let url = NSURL(string: "http://**********/api/academics//getSingleStudentsMarks"+"?StdID=" + std_id_String!)
        var request = URLRequest(url: url! as URL)
        request.httpMethod = "GET"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        Alamofire.request(request).responseJSON() response in
            switch response.result
            case.success(let data):
                print("success",data)

                let myresponse = JSON(data)
                for marks in myresponse.array!
                    let marksObj = Marks(MarksJson: marks)
                    self.markdetail.append(marksObj)

                    self.subjectss.append(marksObj.CourseName)
                    self.marks01.append(marksObj.marks)
//                    for name in markdetail
//                    
//                    

                    self.setname.append(marksObj.examDescription)

                    print("Markss",marksObj.marks)
                    self.forlineView()
                    self.textdisplay()
                


            case.failure(let error):
                print("Not Success",error)
            

        
    


    func forlineView() 
        // 1
        self.lineChartView.delegate = self
        // 2
        self.lineChartView.chartDescription?.text = "Tap node for details"
        // 3
        self.lineChartView.chartDescription?.textColor = UIColor.red
        self.lineChartView.gridBackgroundColor = UIColor.darkGray
//        self.lineChartView.backgroundColor = UIColor.darkGray
        // 4
        self.lineChartView.noDataText = "No data provided"
        // 5
        setChartData(subject: subjectss)

    

    func setChartData(subject : [String]) 

        // 1 - creating an array of data entries
        var yVals1 : [ChartDataEntry] = [ChartDataEntry]()

        for i in 0..<subject.count 
            yVals1.append(ChartDataEntry(x: Double(i), y: Double(marks01[i])))
        

        // 2 - create a data set with our array
        let set1: LineChartDataSet = LineChartDataSet(values: yVals1, label: setname)
        set1.axisDependency = .left // Line will correlate with left axis values
        set1.setColor(UIColor.red.withAlphaComponent(0.5)) // our line's opacity is 50%
        set1.setCircleColor(UIColor.red) // our circle will be dark red
        set1.lineWidth = 2.0
        set1.circleRadius = 6.0 // the radius of the node circle
        set1.fillAlpha = 65 / 255.0
        set1.fillColor = UIColor.red
        set1.highlightColor = UIColor.red
        set1.drawCircleHoleEnabled = true

        //3 - create an array to store our LineChartDataSets
        var dataSets : [LineChartDataSet] = [LineChartDataSet]()
        dataSets.append(set1)

        //4 - pass our months in for our x-axis label value along with our dataSets
        let data: LineChartData = LineChartData(dataSets: dataSets)
        data.setValueTextColor(UIColor.red)

        //5 - finally set our data
        self.lineChartView.data = data

        //6 - add x-axis label
        let xaxis = self.lineChartView.xAxis
        xaxis.valueFormatter = MyXAxisFormatter(subject)

    




    func textdisplay()
        for d in self.markdetail

            self.text.text = d.NAME

        
    




class MyXAxisFormatter: NSObject, IAxisValueFormatter 

    let subjectss: [String]

    init(_ subjects: [String]) 
        self.subjectss = subjects
    

    func stringForValue(_ value: Double, axis: AxisBase?) -> String 
        return subjectss[Int(value) % subjectss.count]
    


目前我正在接受

JSON

 (
        
        CLASSNO = 1;
        "CLASS_ID" = 2021;
        CourseID = 4027;
        CourseName = Arts;
        Marks = 45;
        NAME = "Calvin Patterson";
        Sno = 2107;
        StdID = 95;
        TermID = 6022;
        "Terminal_FM" = 100;
        "Terminal_PM" = 40;
        "UT_FM" = 50;
        "UT_PM" = 20;
        examDescription = "First Term";
        type = Terminal;
    ,
        
        CLASSNO = 1;
        "CLASS_ID" = 2021;
        CourseID = 5034;
        CourseName = Math;
        Marks = 90;
        NAME = "Calvin Patterson";
        Sno = 2108;
        StdID = 95;
        TermID = 6022;
        "Terminal_FM" = 100;
        "Terminal_PM" = 40;
        "UT_FM" = 50;
        "UT_PM" = 20;
        examDescription = "First Term";
        type = Terminal;
    ,
        
        CLASSNO = 1;
        "CLASS_ID" = 2021;
        CourseID = 5035;
        CourseName = English;
        Marks = 52;
        NAME = "Calvin Patterson";
        Sno = 2109;
        StdID = 95;
        TermID = 6022;
        "Terminal_FM" = 100;
        "Terminal_PM" = 40;
        "UT_FM" = 50;
        "UT_PM" = 20;
        examDescription = "First Term";
        type = Terminal;
    ,
        
        CLASSNO = 1;
        "CLASS_ID" = 2021;
        CourseID = 6034;
        CourseName = hi;
        Marks = 70;
        NAME = "Calvin Patterson";
        Sno = 2110;
        StdID = 95;
        TermID = 6022;
        "Terminal_FM" = 100;
        "Terminal_PM" = 40;
        "UT_FM" = 50;
        "UT_PM" = 20;
        examDescription = "First Term";
        type = Terminal;
    ,
        
        CLASSNO = 1;
        "CLASS_ID" = 2021;
        CourseID = 4027;
        CourseName = Arts;
        Marks = 50;
        NAME = "Calvin Patterson";
        Sno = 4119;
        StdID = 95;
        TermID = 7024;
        "Terminal_FM" = 100;
        "Terminal_PM" = 40;
        "UT_FM" = 50;
        "UT_PM" = 20;
        examDescription = Second;
        type = Terminal;
    ,
        
        CLASSNO = 1;
        "CLASS_ID" = 2021;
        CourseID = 5034;
        CourseName = Math;
        Marks = 60;
        NAME = "Calvin Patterson";
        Sno = 4120;
        StdID = 95;
        TermID = 7024;
        "Terminal_FM" = 100;
        "Terminal_PM" = 40;
        "UT_FM" = 50;
        "UT_PM" = 20;
        examDescription = Second;
        type = Terminal;
    ,
        
        CLASSNO = 1;
        "CLASS_ID" = 2021;
        CourseID = 5035;
        CourseName = English;
        Marks = 70;
        NAME = "Calvin Patterson";
        Sno = 4121;
        StdID = 95;
        TermID = 7024;
        "Terminal_FM" = 100;
        "Terminal_PM" = 40;
        "UT_FM" = 50;
        "UT_PM" = 20;
        examDescription = Second;
        type = Terminal;
    ,
        
        CLASSNO = 1;
        "CLASS_ID" = 2021;
        CourseID = 6034;
        CourseName = hi;
        Marks = 80;
        NAME = "Calvin Patterson";
        Sno = 4122;
        StdID = 95;
        TermID = 7024;
        "Terminal_FM" = 100;
        "Terminal_PM" = 40;
        "UT_FM" = 50;
        "UT_PM" = 20;
        examDescription = Second;
        type = Terminal;
    
)

必填

我已经在图表中显示了所有 JSON 数据。我想显示标记和 TermWise 中的主题。如何完成?我想显示标记和 第一学期和第二学期的科目,这些学期的科目相同,但 标记不同,想绘制在同一张图表中

模型类

class Marks 
    var classno = String()
    var class_id = String()
    var  course_id = String()
    var marks = Double()
    var Sno = String()
    var stdid = String()
    var TermID = String()
    var CourseName = String()
    var Terminal_FM = String()
    var Terminal_PM = String()
    var UT_FM = String()
    var UT_PM = String()
    var examDescription = String()
    var type =  String()
    var transferRate = String()
    var NAME = String()

    init(MarksJson:JSON) 
        self.classno = MarksJson["CLASSNO"].stringValue
        self.class_id = MarksJson["CLASS_ID"].stringValue
        self.course_id = MarksJson["CourseID"].stringValue
        self.marks = MarksJson["Marks"].double!
        self.Sno = MarksJson["Sno"].stringValue
        self.stdid = MarksJson["StdID"].stringValue
        self.TermID = MarksJson["TermID"].stringValue
        self.CourseName = MarksJson["CourseName"].stringValue
        self.Terminal_FM = MarksJson["Terminal_FM"].stringValue
        self.Terminal_PM = MarksJson["Terminal_PM"].stringValue
        self.UT_FM = MarksJson["UT_FM"].stringValue
        self.UT_PM = MarksJson["UT_PM"].stringValue
        self.examDescription = MarksJson["examDescription"].stringValue
        self.type =  MarksJson["type"].stringValue
        self.transferRate = MarksJson["transferRate"].stringValue
        self.NAME = MarksJson["NAME"].stringValue

    



【问题讨论】:

“TermWise 中的主题”你能解释一下吗?您的意思是重新组合同一主题的笔记(所有数学,所有英语等)?如果是这种情况,您只需将相应的相加或取平均值即可。 我已经更新了图片并尝试在termid wise中显示标记和主题以在图表中显示不同的线条? 【参考方案1】:
Change your code like this, 




     for marks in myresponse.array!
        
        let marksObj = Marks(MarksJson: marks)
        if marksObj.examDescription == "First Term"
        
        self.markdetail.append(marksObj)
        self.subjectss.append(marksObj.CourseName)
        self.marks01.append(marksObj.marks)
        self.setname = marksObj.examDescription
        print("Markss",marksObj.marks)
        self.forlineView(examDescription: marksObj.examDescription)
        self.textdisplay(marks: marksObj)
        
        else if marksObj.examDescription == "Second Term"
        
        self.markdetailForSecondTerm.append(marksObj)
        self.subjectssForSecondTerm.append(marksObj.CourseName)
        self.marks01ForSecondTerm.append(marksObj.marks)
        self.setname = marksObj.examDescription
        print("Markss",marksObj.marks)
        self.forlineView(examDescription: marksObj.examDescription)
        self.textdisplay(marks: marksObj)
        
        
    

    func forlineView(examDescription: String)
    
        if examDescription ==  "First Term"
        
            // 1
            self.lineChartView.delegate = self
            // 2
            self.lineChartView.chartDescription?.text = "Tap node for details"
            // 3
            self.lineChartView.chartDescription?.textColor = UIColor.red
            self.lineChartView.gridBackgroundColor = UIColor.darkGray
            //        self.lineChartView.backgroundColor = UIColor.darkGray
            // 4
            self.lineChartView.noDataText = "No data provided"
            // 5
            setChartData(subject: subjectss,marks: marks01,exam: examDescription)
        
        else if examDescription ==  "Second Term"
        
            // 1
            self.lineChartView.delegate = self
            // 2
            self.lineChartView.chartDescription?.text = "Tap node for details"
            // 3
            self.lineChartView.chartDescription?.textColor = UIColor.green
            self.lineChartView.gridBackgroundColor = UIColor.darkGray
            //        self.lineChartView.backgroundColor = UIColor.darkGray
            // 4
            self.lineChartView.noDataText = "No data provided"
            // 5
            setChartData(subject: subjectssForSecondTerm,marks: marks01ForSecondTerm,exam: examDescription)
        
    

    func setChartData(subject : [String], marks : [Double], exam: String) 

    // 1 - creating an array of data entries
    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    if (exam == "First Term")
    

        for i in 0..<subject.count 
            yVals1.append(ChartDataEntry(x: Double(i), y: Double(marks[i])))
        
        // 2 - create a data set with our array
        let set1: LineChartDataSet = LineChartDataSet(values: yVals1, label: "First Term")
        set1.axisDependency = .left // Line will correlate with left axis values
        set1.setColor(UIColor.red.withAlphaComponent(0.5)) // our line's opacity is 50%
        set1.setCircleColor(UIColor.red) // our circle will be dark red
        set1.lineWidth = 2.0
        set1.circleRadius = 6.0 // the radius of the node circle
        set1.fillAlpha = 65 / 255.0
        set1.fillColor = UIColor.red
        set1.highlightColor = UIColor.red
        set1.drawCircleHoleEnabled = true

        //3 - create an array to store our LineChartDataSets
        dataSets.append(set1)

        //4 - pass our months in for our x-axis label value along with our dataSets

    

    else if (exam == "Second Term")
    
        var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
        for i in 0..<subject.count 
            yVals2.append(ChartDataEntry(x: Double(i), y: Double(marks[i])))
        
        // 2 - create a data set with our array
        let set2: LineChartDataSet = LineChartDataSet(values: yVals2, label: "Second Term")
        set2.axisDependency = .left // Line will correlate with left axis values
        set2.setColor(UIColor.red.withAlphaComponent(0.5)) // our line's opacity is 50%
        set2.setCircleColor(UIColor.red) // our circle will be dark red
        set2.lineWidth = 2.0
        set2.circleRadius = 6.0 // the radius of the node circle
        set2.fillAlpha = 65 / 255.0
        set2.fillColor = UIColor.red
        set2.highlightColor = UIColor.red
        set2.drawCircleHoleEnabled = true

        //3 - create an array to store our LineChartDataSets
        dataSets.append(set2)


    

    let data: LineChartData = LineChartData(dataSets: dataSets)
    data.setValueTextColor(UIColor.red)

    //5 - finally set our data
    self.lineChartView.data = data

    //6 - add x-axis label
    let xaxis = self.lineChartView.xAxis
    xaxis.valueFormatter = MyXAxisFormatter(subject)



    func textdisplay(markDetails: [Marks])
        for d in self.markDetails

            self.text.text = d.NAME

        
    

【讨论】:

我得到的结果与我之前的屏幕截图相同 或者我们是否需要更改 setChartData 中的任何内容 是的,我正在尝试这样两个术语标记应该显示在不同的行中 我已经创建了模型类并在上面更新了它,是好还是我需要创建另一个模型类以及如何为每个术语分离 NSMutableArrays 我已经创建了它,所以我在数组字符串中附加 Marks 数据

以上是关于如何在网页中显示数据图表的主要内容,如果未能解决你的问题,请参考以下文章

如何使用canvasJS在PHP中制作动态图表?

如何使用canvasJS在PHP中制作动态图表?

labview如何调用漂亮的可视化图表

如何在谷歌图表上使用mysql数据

柱状图怎么显示数字

怎么在excel图表上既显示柱状图又显示折线图