js生成折线图(一条折线或多条折线)

这篇博客介绍了如何使用JavaScript生成折线图,包括一条或多条折线。代码示例展示了一个自定义的函数`table2`,它接受多个参数,用于创建折线图。博客中提到了JavaScript和C#的标签,可能涉及这两个语言在网页交互中的应用。

参照 http://hi.baidu.com/nkiqzedjqwadpre/item/75dc612087b1530877272c4d ,自己稍微改进了一下。

代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HCXYPlanTB1.aspx.cs" Inherits="Modules_Plan_HCXYPlanTB1" %>

<!-- 代码开始===========================================-->
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<!--[if !mso]>
<style>
v\:*       { behavior: url(#default#VML) }
o\:*       { behavior: url(#default#VML) }
.shape     { behavior: url(#default#VML) }
</style>
<![endif]-->
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
  <title></title>
  <style type="text/css">
    TD
    { FONT-SIZE: 9pt}
  </style>

  <script language="javascript" type="text/javascript">

  function table2(total,table_x,table_y,all_width,all_height,line_no)
  {
   
  //参数含义(传递的数组,横坐标,纵坐标,图表的宽度,图表的高度,折线条数)
  //生成图表函数2——折线图

   
    var line_color = "#69f";
    var left_width = 70;
    var total_no = total[1].length
   
    var temp1,temp2,temp3
    temp1 = 0;
    for(var i=1;i<total_no;i++)
    {
        for(var j=1;j<=line_no;j++)
        {
          if(temp1<total[j][i])
          temp1 = total[j][i];
        }
    }
    temp1 = parseInt(temp1);
    if(temp1>9)
    {
        temp2 = temp1.toString().substr(1,1);
        if(temp2>4)
        {
          temp3 = (temp1/(Math.pow(10,(temp1.toString().length-1)))+1)*Math.pow(10,(temp1.toString().length-1));
        }
        else
        {
          temp3 = (temp1/(Math.pow(10,(temp1.toString().length-1)))+0.5)*Math.pow(10,(temp1.toString().length-1));
        }
    }
    else
    {
        if(temp1>4)
        {
          temp3 = 10;
        }
        else
        {
          temp3 = 5;
        }
    }
    temp4 = temp3;
    document.write("<v:rect id='_x0000_s1027' alt='' style='position:absolute;left:" + (table_x + left_width) + "px;top:" + table_y + "px;width:" + all_width + "px;height:" + all_height + "px;z-index:-1' fillcolor='#9cf' stroked='f'><v:fill rotate='t' angle='-45' focus='100%' type='gradient'/></v:rect>");
    for(var i=0;i<all_height;i += all_height/5)
    {
        document.write("<v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + left_width + length) + "px," + (table_y + all_height - length - i) + "px' to='" + (table_x + all_width + left_width) + "px," + (table_y + all_height - length - i) + "px' strokecolor='" + line_color + "'/>");
        document.write("<v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + (left_width - 15)) + "px," + (table_y + i) + "px' to='" + (table_x + left_width) + "px," + (table_y + i) + "px'/>");
        document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + table_x + "px;top:" + (table_y + i) + "px;width:" + left_width + "px;height:18px;z-index:1'>");
        document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='right'>" + temp4 + "</td></tr></table></v:textbox></v:shape>");
        temp4 = temp4 - temp3/5;
    }
   
    document.write("<v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + left_width) + "px," + (table_y + all_height) + "px' to='" + (table_x + all_width + left_width) + "px," + (table_y + all_height) + "px'/>");
    document.write("<v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + left_width) + "px," + table_y + "px' to='" + (table_x + left_width) + "px," + (table_y + all_height) + "px'/>");
   
    var tmpStr = ""
    for(i=1;i<=line_no;i++)
    {
        var re   = /,/g;
        tmpStr += ",[\"" + total[i][0].replace(re,"\",\"") + "\"]"
    }
    tmpStr = tmpStr.substr(1,tmpStr.length-1)
    var line_code = eval("new Array(" + tmpStr + ")")
    for(var j=1;j<=line_no;j++)
    {
        for(var i=1;i<total_no-1;i++)
        {

        var x1 = table_x + left_width + all_width * (i - 1)/(total_no-1)
        var y1 = table_y + (temp3 - total[j][i]) * (all_height/temp3)  
        var x2 = table_x + left_width + all_width * i/(total_no-1)
        var y2 = table_y + (temp3 - total[j][i+1]) * (all_height/temp3)
        var x3 = table_x + left_width + all_width *   (total_no-1)/total_no
        var y3 = table_y + (temp3 - total[line_no][total_no-1]) * (all_height/temp3)
       
      
       
        document.write("<span style='position:absolute; margin-left:"+x1+"px;margin-top:"+(y1-20)+"px; color:#FF0000; font-size:12px;'>"+total[j][i]+"</span>");
        document.write("<span style='position:absolute; margin-left:"+(x3-10)+"px;margin-top:"+(y3-20)+"px; color:#FF0000; font-size:12px;'>"+total[line_no][total_no-1]+"</span>");

        document.write("<v:line id='_x0000_s1025' alt='' style='position:absolute;left:0;text-align:left;top:0;z-index:1' from='" + x1 + "px," + y1 + "px' to='" + x2 + "px," + y2 + "px' coordsize='21600,21600' strokecolor='" + line_code[j-1][0] + "' strokeweight='" + line_code[j-1][1] + "'>");
         
        switch (parseInt(line_code[j-1][2]))
        {
            case 1:
            break;
            case 2:
            document.write("<v:stroke dashstyle='1 1'/>");
            break;
            case 3:
            document.write("<v:stroke dashstyle='dash'/>");
            break;
            case 4:
            document.write("<v:stroke dashstyle='dashDot'/>");
            break;
            case 5:
            document.write("<v:stroke dashstyle='longDash'/>");
            break;
            case 6:
            document.write("<v:stroke dashstyle='longDashDot'/>");
            break;
            case 7:
            document.write("<v:stroke dashstyle='longDashDotDot'/>");
            break;
        }
        document.write("</v:line>");
        switch (parseInt(line_code[j-1][3]))
        {
            case 1:
            break;
            case 2:
            document.write("<v:rect id='_x0000_s1027' style='position:absolute;left:" + (x1 - 2) + "px;top:" + (y1 - 2) + "px;width:4px;height:4px; z-index:2' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>");
            break;
            case 3:
            document.write("<v:oval id='_x0000_s1026' style='position:absolute;left:" + (x1 - 2) + "px;top:" + (y1 - 2) + "px;width:4px;height:4px;z-index:1' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>");
            break;
        }

        }
        switch (parseInt(line_code[j-1][3]))
        {
          case 1:
          break;
          case 2:
          if(line_no==1&&total_no==2)
          {
                document.write("<v:rect id='_x0000_s1027' style='position:absolute;left:" + ((table_x + left_width)-2) + "px;top:" + (table_y + (temp3 - total[line_no][line_no]) * (all_height/temp3))    + "px;width:4px;height:4px; z-index:2' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>");
                document.write("<span style='position:absolute; margin-left:"+(table_x + left_width)+"px;margin-top:"+((table_y + (temp3 - total[line_no][line_no]) * (all_height/temp3)   )-20)+"px; color:#FF0000; font-size:12px;'>"+total[line_no][line_no]+"</span>");
          }
          else
          {
                document.write("<v:rect id='_x0000_s1027' style='position:absolute;left:" + (x2 - 2) + "px;top:" + (y2 - 2) + "px;width:4px;height:4px; z-index:2' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>");
          }
          break;
          case 3:
          document.write("<v:oval id='_x0000_s1026' style='position:absolute;left:" + (x2 - 2) + "px;top:" + (y2 - 2) + "px;width:4px;height:4px;z-index:1' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>");
          break;
        }
    }
    for(var i=0;i<total_no-1;i++)
    {
        document.write("<v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + left_width + all_width * (i)/(total_no-1)) + "px," + (table_y + all_height) + "px' to='" + (table_x + left_width + all_width * (i)/(total_no-1)) + "px," + (table_y + all_height + 15) + "px'/>");
        document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x + left_width + all_width * (i)/(total_no-1)) + "px;top:" + (table_y + all_height) + "px;width:" + (all_width/(total_no-1)) + "px;height:18px;z-index:1'>");
        document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='left'>" + total[0][i] + "</td></tr></table></v:textbox></v:shape>");
    }
   
    var tb_height = 30
    document.write("<v:rect id='_x0000_s1025' style='position:absolute;left:"+(table_x+all_width+20)+"px;top:"+table_y + "px;width:100px;height:" + (line_no * tb_height + 20) + "px;z-index:1'/>");
    for(var i=0;i<line_no;i++)
    {
        document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x + all_width + 25) + "px;top:" + (table_y + 10+(i) * tb_height) + "px;width:60px;height:" + tb_height + "px;z-index:1'>");
        document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='left'>" + line_code[i][4] + "</td></tr></table></v:textbox></v:shape>");
        document.write("<v:rect id='_x0000_s1040' alt='' style='position:absolute;left:" + (table_x + all_width + 80 ) + "px;top:" + (table_y + 10 + (i) * tb_height + 4) + "px;width:30px;height:20px;z-index:1' fillcolor='" + line_code[i][0] + "'><v:fill color2='" + line_code[i][0] + "' rotate='t' focus='100%' type='gradient'/></v:rect>");
    }
  }
  </script>

</head>
<body style="margin-top: 5px; margin-left: 0px; overflow: auto;">
  <form id="form1" runat="server">
    <asp:HiddenField ID="hidBillNo" runat="server" />
    <asp:HiddenField ID="hidSHDate" runat="server" />
    <asp:HiddenField ID="hidSHDJ" runat="server" />
    <asp:HiddenField ID="hidMaName" runat="server" />

    <script language="javascript" type="text/javascript">

    var strSHDate = document.getElementById("hidSHDate").value;
    var date = strSHDate.split(","); 
    var strSHDJ = document.getElementById("hidSHDJ").value;
    var dj= strSHDJ.split(",");
    var dateArray = new Array();
    var djArray   = new Array();
    for (var i = 0;i<date.length-1;i++)
    {
        for( var j = 0;j<dj.length-1;j++)
        {
            if(i==j)
            {
                djArray[0]="#FF0000,1.5,1,2,"+document.getElementById("hidMaName").value+""
                djArray[i+1] = parseInt(dj[i])
                dateArray[i] = date[i]
            }
        }
    }
    var total = new Array(dateArray,djArray)    //参数含义(时间(横坐标),单价(纵坐标))
    table2(total,50,50,strSHDJ.length*40,250,1);    //参数含义(传递的数组,横坐标,纵坐标,图表的宽度,图表的高度,折线条数)
    </script>

  </form>
</body>
</html>
<!-- 代码结束===========================================-->

转载于:https://www.cnblogs.com/xiayang123/archive/2012/10/30/2746466.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值