动态生成折线图

public   class  PrintTable : System.Web.UI.Page
    {
        
private   void  Page_Load( object  sender, System.EventArgs e)
        {
            
if (Session[ " chuzulv " ] != null )
            {
                
// 数据源
                Table table = (Table)Session[ " Chuzulv " ];

                
// 可修改属性
                 int  Height = 300 ;                                 // 图像高度
                 int  Width = 500 ;                                 // 图像宽度
                 string  TableName = " 物资出租率 " ;                 // 表头
                Font TableHeaderFont = new  Font( " 宋体 " , 12 );     // 表头字体
                 float  High = 100 ;                                 // 默认最大标尺
                 float  Low = 0 ;                                 // 默认最小标尺
                Color BGColor = Color.Black;                     // 背景色
                Color HLineColor = Color.DarkCyan;             // 横向表格线颜色
                Color LineColor = Color.WhiteSmoke;             // 曲线颜色

                
// 临时属性不可修改
                Bitmap bm  =   new  Bitmap(Width,Height);         // 图像对象
                Graphics g  =  Graphics.FromImage(bm);         // 绘图对象
                StringFormat drawFormat  =   new  StringFormat(); // 纵向文字输出格式
                drawFormat.FormatFlags  =  StringFormatFlags.DirectionVertical;

                SolidBrush br
= new  SolidBrush(Color.Red);     // Brush,红色
                Pen p  = new  Pen(Color.Red, 1 );                 // Pen,红色,宽度1
                g.Clear(BGColor);                             // 绘制背景色
                g.DrawString(TableName,TableHeaderFont,br, new  Point( 0 , 0 ));  // 绘制表头
                 int  HStep  =  (Width  -   30 /  (table.Rows.Count  -   1 );                 // 横向步长(每日期)
                 int  VStep;                                     // 纵向步长(每百分点)
                 int  index = 0 ;                                 // 临时标示记录索引
                 foreach (TableRow r  in  table.Rows)
                {
                    index
++ ;
                    
if  (index  ==   1 )                             // 过滤数据源第一行Header
                         continue ;
                    
if  (r.Cells[ 5 ].Text  !=   ""   &&   float .Parse(r.Cells[ 5 ].Text)  >  High)     // 重新获取纵座标上下限
                        High  =   float .Parse(r.Cells[ 5 ].Text);
                    
if  (r.Cells[ 5 ].Text  !=   ""   &&   float .Parse(r.Cells[ 5 ].Text)  <  Low)
                        Low 
=   float .Parse(r.Cells[ 5 ].Text);
                }
                High 
+=  (High  %   10   ==   0 ?   0  :  10   -  High  %   10 ; // 上下限对齐
                Low  -=  (Low  %   10   ==   0 ?   0  :  10   +  Low  %   10 ;
                VStep 
=  (Height  -   50 /  ( int )(High  -  Low);     // 获取纵向步长

                Point lineStartPoint;                        
// 曲线起始点
                Point lineEndPoint;                             // 曲线终止点
                 int  BaseVHeight = 0 ;                             // 0点纵坐标
                 int  Vertical = 30 ;                             // 横向参考线最高点
                 for ( float  HLine  =  High ; HLine  >=  Low ;HLine  -=   10 )         // 绘制横向辅助线
                {
                    g.DrawLine(
new  Pen(Color.DarkCyan, 1 ), new  Point( 20 ,Vertical), new  Point(Width,Vertical));
                    g.DrawString(HLine.ToString()
+ " % " , new  Font( " 宋体 " , 8 ),br, new  Point( 5 ,Vertical  -  VStep ));
                    
if (HLine == 0 )                            
                        BaseVHeight
= Vertical;
                    Vertical 
+=  VStep  *   10 ;
                }
                Point startPoint
= new  Point( 0 ,Height - 50 );
                startPoint.X 
+=   10  ;
                
                lineStartPoint
= new  Point(startPoint.X   +  HStep  /   2 ,BaseVHeight);

                index 
=   0 ;
                
float  PerCent = 0 ;
                
foreach (TableRow r  in  table.Rows)
                {
                    index
++ ;
                    g.DrawString(r.Cells[
0 ].Text, new  Font( " 宋体 " , 8 ),br,startPoint,drawFormat);
                    
if  (index  ==   1 )
                        startPoint.X 
+=   20 ;
                    
else
                        startPoint.X 
+=  HStep;
                    
if  (r.Cells[ 5 ].Text  !=   "" )
                        
if  (index  ==   1 )
                            
continue ;
                        
else  
                            PerCent 
=   float .Parse(r.Cells[ 5 ].Text);
                    
else
                        PerCent 
=   0 ;
                    
if  (index  ==   2 )
                        lineStartPoint
= new  Point(startPoint.X  -  HStep ,BaseVHeight  -  ( int )PerCent  *  VStep);
                        
                    lineEndPoint 
=   new  Point(startPoint.X  -  HStep,BaseVHeight  -  ( int )PerCent  *  VStep);
                    g.DrawLine(
new  Pen(Color.WhiteSmoke, 1 ),lineStartPoint,lineEndPoint);
                    
                    lineStartPoint 
=  lineEndPoint;
                }
                
// 输出
                bm.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
                
// 清除对象
                ((Table)Session[ " chuzulv " ]).Dispose();
                Session[
" chuzulv " ] = null ;
            }
        }

补充一下Session[]==null;无法清除Session对象
还要用Session.Remove(*)来清除

因为报表不需要交互,所以一直用Table,

前一个页统计出Table之后存入Session["chuzulv"],然后置一Image控件,ImageUrl属性指向此页面

效果如图,上传时压缩了品质,动态生成的Gif效果很不错

  

前一页面Image控件的ImageUrl="PrintTable.aspx"

源码就是这样了,主要流程就是-->获取数据-->根据数据生成坐标轴-->画线

因为这个东西在项目里面一带而过,所以没有把它的功能扩展封装。
起个头,有兴趣自己扩展吧。
Plotly是一个强大的数据可视化工具库,可以用来动态生成折线图。使用Plotly创建动态折线图的步骤如下: 1. 导入所需的库和模块。首先,您需要导入Plotly库和相关的模块。可以使用以下代码导入: ```python import plotly.graph_objs as go from plotly.offline import plot ``` 2. 创建数据。创建包含折线图数据的列表。例如,您可以创建一个包含x轴和y轴数据的列表: ```python x = [1, 2, 3, 4, 5] y = [10, 15, 7, 12, 9] ``` 3. 创建折线图布局。使用`go.Layout()`函数创建折线图的布局,并设置标题、轴标签等参数。例如: ```python layout = go.Layout( title='动态生成折线图', xaxis=dict(title='X轴'), yaxis=dict(title='Y轴') ) ``` 4. 创建折线图轨迹。使用`go.Scatter()`函数创建折线图的轨迹,并设置x轴和y轴数据。例如: ```python trace = go.Scatter( x=x, y=y, mode='lines', name='折线图' ) ``` 5. 组合数据和布局。将轨迹和布局组合在一起,创建一个数据列表。例如: ```python data = [trace] ``` 6. 绘制折线图。使用`plot()`函数将数据和布局传递给Plotly库,并生成折线图。例如: ```python fig = go.Figure(data=data, layout=layout) plot(fig, filename='动态折线图.html') ``` 这样,您就可以使用Plotly动态生成折线图了。您可以将生成的图表保存为HTML文件,并在浏览器中查看。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python折线图样式_用Python的Plotly画出炫酷的数据可视化(含各类图介绍)](https://blog.csdn.net/weixin_39607240/article/details/110109686)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值