设为首页 加入收藏

TOP

Redis源码分析(五)---sparkline微线图(二)
2015-07-24 11:16:02 来源: 作者: 【 】 浏览:11
Tags:Redis 源码 分析 ---sparkline 线图
turn SDS strings instead of outputting directly to * the terminal. * * --------------------------------------------------------------------------- 在sparkline.c中的注释声明,此代码修改自 http://github.com/antirez/aspark,原来是开源的代码实现,但是一开始真的不知道还有这么个叫aspark的东西,都跟BigData里的spark搞混了,然后我点击此地址,官方解释来了:
aspark is a C program to display ASCII Sparklines.
It is completely useless in 2011.
不错,意思就是说aspark就是用来在C程序上显示图线效果的。后来,我看了下,的确代码差不多,redis的代码在上面加了自己的东西,稍稍修改,aspark的图线展现有几种形式,第一种,最简单的展示:
$ ./aspark 1,2,3,4,10,7,6,5
    `-_ 
__-`   `
第二张把行数扩展为更多行,展示更多的数据,上面的这个为2行展示,数据多的时候,调节行数,默认输出2行展示

?

$ ./aspark 1,2,3,4,5,6,7,8,9,10,10,8,5,3,1 --rows 4
       _-``_   
     _`        
   -`       `  
_-`          `_
当然可以更加可视化,在空白处填充字符,看起来更舒服:??
$ ./aspark 1,2,3,4,5,6,7,8,9,10,10,8,5,3,1 --rows 4 --fill
       _o##_   
     _#|||||   
   o#|||||||#  
_o#||||||||||#_
用了"|"符号,很有想象力的哦,最最关键的我们看如何实现这样的效果呢,核心代码如下:
/* Render part of a sequence, so that render_sequence() call call this function
 * with differnent parts in order to create the full output without overflowing
 * the current terminal columns. */
/* 渲染出这个图线信息 */
sds sparklineRenderRange(sds output, struct sequence *seq, int rows, int offset, int len, int flags) {
    int j;
    double relmax = seq->max - seq->min;
    int steps = charset_len*rows;
    int row = 0;
    char *chars = zmalloc(len);
    int loop = 1;
    int opt_fill = flags & SPARKLINE_FILL;
    int opt_log = flags & SPARKLINE_LOG_SCALE;

    if (opt_log) {
        relmax = log(relmax+1);
    } else if (relmax == 0) {
        relmax = 1;
    }

    while(loop) {
        loop = 0;
        memset(chars,' ',len);
        for (j = 0; j < len; j++) {
            struct sample *s = &seq->samples[j+offset];
            //value派上用处了
            double relval = s->value - seq->min;
            int step;

            if (opt_log) relval = log(relval+1);
            //最后会算出相关的step
            step = (int) (relval*steps)/relmax;
            if (step < 0) step = 0;
            if (step >= steps) step = steps-1;

            if (row < rows) {
                /* Print the character needed to create the sparkline */
                /* step控制输出的字符是哪一个 */
                int charidx = step-((rows-row-1)*charset_len);
                loop = 1;
                if (charidx >= 0 && charidx < charset_len) {
                    chars[j] = opt_fill ? charset_fill[charidx] :
                                          charset[charidx];
                } else if(opt_fill && charidx >= charset_len) {
                	//用"|"填充内容,更加可视化
                    chars[j] = '|';
                }
            } else {
                /* Labels spacing */
                if (seq->labels && row-rows < label_margin_top) {
                    loop = 1;
                    break;
                }
                /* Print the label if needed. */
                if (s->label) {
                    int label_len = strlen(s->label);
                    int label_char = row - rows - label_margin_top;

                    if (label_len > label_char) {
                        loop = 1;
                        chars[j] = s->label[label_char];
                    }
                }
            }
        }
        if (loop) {
            row++;
            output = sdscatlen(output,chars,len);
            output = sdscatlen(output,"\n",1);
        }
    }
    zfree(chars);
    return output;
}
由于本人能力有限,有点不太懂里面的具体细节,大概看了下,把变量用到的地方稍稍看了下,上面的代码都是非常优秀的代码,值得我们学习,今天至少让我知道了什么叫sparkline叫什么 了,哈哈。?
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇大数据应用的未来挑战和发展趋势 下一篇awk常见用法总结

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++ Lambda表达式保 (2025-12-26 05:49:45)
·C++ Lambda表达式的 (2025-12-26 05:49:42)
·深入浅出 C++ Lambda (2025-12-26 05:49:40)
·C语言指针从入门到基 (2025-12-26 05:21:36)
·【C语言指针初阶】C (2025-12-26 05:21:33)