美文网首页
项目涉及知识之折线绘制

项目涉及知识之折线绘制

作者: 狗娃_ | 来源:发表于2017-08-01 14:59 被阅读0次

大概需求:

两条折线图。
实线:数据是有值则画线,值为0时则跳过,直到下一个点有值;
虚线:间隔7天有值,值为0时则跳过,直到下一个点有值。
日期:当前日期居中,滑动折线图时,日期跟着滑动。
滑动效果:以实线中两个有值点的距离作为一个pageSize,滑动回弹。

设计图:

设计图1.png 设计图2.png

基础知识:

1.实线的绘制方式:

    CGContextRef context = UIGraphicsGetCurrentContext(); // 获取图形上下文环境
    CGContextSetLineWidth(context, _reallineWidth);  // 设置线宽
    CGContextSetLineJoin(context, kCGLineJoinRound);// 设置线条的转角的样式
    CGContextSetLineCap(context, kCGLineCapRound);// 设置图形环境中的画线的端点样式
    CGContextMoveToPoint(context, initialPoint.x, initialPoint.y); // 移动到初始点
    CGContextAddLineToPoint(context, obj.realline_x, obj.realline_y) 
    CGContextStrokePath(context); // 绘制

2.虚线的绘制方式

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, _dashlineWidth);
    [_dashlineColor set];
    CGFloat lengths[] = {4,4}; 
    CGContextSetLineDash(context, 0, lengths, 2); // 设置虚线模式
    CGPoint initialPoint = CGPointMake(firstPoint.dashline_x, firstPoint.dashline_y);
    CGContextMoveToPoint(context, initialPoint.x, initialPoint.y); 
    CGContextStrokePath(context);

3.日期(CATextLayer)

    CATextLayer *dateLayer = [CATextLayer layer];
    dateLayer.size = CGSizeMake(kLTLineViewDateWidth, kLTLineViewDateHeight);
    dateLayer.center = CGPointMake(obj.date_x, obj.date_y);
    dateLayer.alignmentMode = kCAAlignmentCenter;
    dateLayer.font = (__bridge CFTypeRef)(_dateFont);
    dateLayer.fontSize = -_dateFont.pointSize;
    dateLayer.string = obj.date;
    dateLayer.foregroundColor = _dateColor.CGColor;
    dateLayer.contentsScale = [UIScreen mainScreen].scale;
    [self.layer addSublayer:dateLayer];

4.Scrollview滑动回弹

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    // scroll自定义page回弹size
    CGFloat kMaxIndex = (self.lineScollView.contentSize.width-kScreenWidth)/(kLTLineViewDateSpace+kLTLineViewDateWidth);
    CGFloat targetX = scrollView.contentOffset.x + velocity.x * (kLTLineViewDateSpace+kLTLineViewDateWidth);
    CGFloat targetIndex = round(targetX / (kLTLineViewDateSpace+kLTLineViewDateWidth));
    if (targetIndex < 0)
        targetIndex = 0;
    if (targetIndex > kMaxIndex)
        targetIndex = kMaxIndex;
    targetContentOffset->x = targetIndex * (kLTLineViewDateSpace+kLTLineViewDateWidth);
}

具体实现:

先看看结构示例:

结构示例.png
代码地址:
https://github.com/Cherishforever/LTLineView

相关文章

  • 项目涉及知识之折线绘制

    大概需求: 两条折线图。实线:数据是有值则画线,值为0时则跳过,直到下一个点有值;虚线:间隔7天有值,值为0时则跳...

  • SVG 绘制曲折线

    本节我们来学习如何在 SVG 中绘制曲折线,绘制曲折线可以使用 元素来实现。 如何绘制曲折线 曲折线就是通过一...

  • 第01章:ScottPlot.NET 折线图

    一、概述 本文介绍使用ScottPlot.WPF绘制折线图。 二、折线图 第一步:新建项目 1.新建项目:SPLi...

  • matplotlib快速入门

    1. 折线图 1.1 绘制一条折线 图形如下: 1.2绘制两条折线的方法: 图形: 1.3 绘制多条曲线 1.4 ...

  • 2018-10-15

    绘制折线图,折线叠加图,柱状叠加图。

  • charts 写柱状图和折线图

    绘制柱状图 绘制折线图

  • R语言可视化(二):折线图绘制

    02.折线图绘制 清除当前环境中的变量 设置工作目录 base plot函数绘制普通折线图 ggplot2包绘制带...

  • SVG

    SVG 绘制长方形绘制圆形绘制椭圆 绘制直线 绘制折线 绘制多边形 ...

  • 数据处理pandas,matplot简单用法

    pandas导入文件 matplot基础 折线图散点图使用plot模块来作图 绘制折线图 绘制散点图 样式 点的颜...

  • 绘制折线

    CGContextRef context = UIGraphicsGetCurrentContext(); CGC...

网友评论

      本文标题:项目涉及知识之折线绘制

      本文链接:https://www.haomeiwen.com/subject/aftrkxtx.html