我的成长磨练,每天写博客,年轻人,卷起袖子,来把手弄脏吧! ------ 博客首页

echarts 图 折线图、热力图添加趋势拟合线,ecStat

线性拟合,

回归算法如何把数据分析与echarts 图表结合

 echarts 的一个扩展库:echarts-stat.js

ecStat 是 ECharts 的统计和数据挖掘工具。

直接引用或者下载:

1
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-stat/ecStat.min.js"></script>

  调用:

1
var myRegression = ecStat.regression(regressionType, data, order);

  

参数说明
regressionType:回归类型(String),有四种回归算法类型:'linear', 'exponential', 'logarithmic', 'polynomial'
data:要统计的数据,是个二维数组(Array),一维数组值分别表示自变量和因变量的值。
order:多项式的阶数(number)。对于非多项式回归,可以忽略该参数。
 
返回值说明
myRegression:返回一个对象. 包括用于绘制折线图的拟合数据点 points,回归曲线的参数 parameter,以及拟合出的曲线表达式 expression。如下:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 绘制折线图的拟合数据点
 myRegression.points = [
    [1, 2],
    [3, 4],
    ...
 ];
 
 // 这是线性回归到额参数,对于其它的回归类型,返回值有所不同
 myRegression.parameter = {
    gradient: 1.695,
    intercept: 3.008
 };
 
// 拟合曲线的表达式
 myRegression.expression = 'y = 1.7x + 3.01';

  示例数据:

复制代码
 <table id="tblSearch" class="search_all" style="width:100%;display:block;">
        <tr>
            <td id="haha" width="50%" style="display:block;">        
                <!-- 量值溯源误差趋势 -->
                <div class="spDiv" style="position:auto;height: 450px;width:100%;">
                <div id="showLedgerCountBar" style="height:100%;width:100%;"></div>
                </div>
            </td>
        </tr>
    </table>
   
   <script language="javascript">
   var dataEcstat = [
    [1990, 97.50795611],
    [1991, -1.47594249]
    ]
    init();
    
    function init(){
        ledgerCountBar();
    }
    

    function ledgerCountBar(){
        var ledgerCount = echarts.init(document.getElementById('showLedgerCountBar'));

       
        var regressionDemo = ecStat.regression( "linear", dataEcstat, 1);

        
        
        option = {
                title: {
                text: ' 折线图',
                            subtext: ' ',
                            left: 'center'
                        },
                 tooltip: {
                            trigger: 'item',
                            formatter: '{a} <br/>{b} : {c}'
                        },
                xAxis: {
                    type: 'category'
                   
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                     name: '数据',
                     label: {
                                    show: true,
                                    position: "top",
                                     formatter:' {c}'
                                  },
                    data: dataEcstat,
                    type: 'line'
                },
                {
                     name: '误差趋势拟合',
                     label: {
                                    show: true,
                                    position: "top",
                                     formatter:' {c}'
                                  },
                    data:  regressionDemo.points,
                    lineStyle: {
                       color: "#f00"
                   },
                    type: 'line'
                }
                ]
            };

        ledgerCount.setOption(option);
        window.onresize = ledgerCount.resize;
    }
    

    
    
</script>
复制代码

参考:https://www.jianshu.com/p/c97273a05167

 
 
posted @   ltian123  阅读(5170)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
go顶部
点击右上角即可分享
微信分享提示