Redis Learning Note (8)---Micro-line diagram Sparkline

Source: Internet
Author: User

Sparkline

A micro-line graph, a graph formed with a single point of information, similar to a line chart. When the delay is expressed, it can be expressed in the form of a micro-line graph.

Example:

Information points in a micro-line chart can be in two forms: "_-" and "_o#"
The resulting micro-line plots in both cases are as follows:
     
You can see, with a single point of information, very visually and clearly formed the desired line chart

The realization of Sparkline

Each information point in the Sparkline is defined as:

struct sample {    doublevalue;    char *label;};

Value for this point
Label as name
And the final micro-line graph is realized by this kind of information point sequence.

struct sequence {    intlength;    int labels;    structsample *samples;    doubleminmax;};
  • Length: The number of samples in the sequence
  • Labels: all labeled sample points in a sequence
  • Samples: An array of sample points that make up a micro-line graph
  • Min: Minimum value in all sample points
  • Max: The maximum value in all the sample points
Create a sequence of the micro-line graphs
struct sequence *createSparklineSequence(void) {    struct sequence *seq = zmalloc(sizeof(*seq));    0;    NULL;    return seq;}
Adding a spline to a micro-line graph
voidSparklinesequenceaddsample (structSequence *SEQ,DoubleValue, char *label) {label = (label = = NULL | | label[0] = = ' \0‘) ? Null:zstrdup (label);if(seq->length==0) {seq->min= seq->Max= value; }Else{//Update min and Max values        if(Value < seq->min) seq->min= value;Else if(Value > Seq->Max) seq->Max= value; } seq->samples = Zrealloc (seq->samples,sizeof (struct Sample) * (seq->length+1)); Seq->samples[seq->length].value = value;//Add sample points to the sample point setSeq->samples[seq->length].label = label; Seq->length++;if(label) seq->labels++;}

The sample points are added to the sequence set in a certain order, so the x-coordinates of all the samples in the resulting micro-line graph are incremented sequentially.

Drawing
sds sparklinerender (SDS output, struct sequence *seq , int  columns, int     Rows, int  flags) {int  J; for  (j = 0 ; J < Seq->length ; j + = columns) {int  Sublen = (seq->length -j) < columns ?        (Seq->length -j): columns; if  (J! = 0 ) output = Sdscatlen (Output,, 1 );    Output = Sparklinerenderrange (output, SEQ, rows, J, Sublen, flags); } return  output;}  

Finally, all of the sample points are converted to ASCII form by calling functions to form the final line chart.
Parameters columns and rows limit the number of rows and columns of the last output graph, and the parameter flags to select how each information point is represented.
When the number of samples in SEQ is not greater than the Qualified column count, you can convert each of the samples in the sequence to the form of a string by simply performing a sparklinerenderrange ().
Finally, the output line chart is stored as a string in output.

//Seq->samples[offset,offset+len in SEQ is converted to rows in the form of rows string, stored in outputSDS Sparklinerenderrange (SDS output,structSequence *SEQ,intRowsintOffsetintLenintFlags) {intJ Double Relmax = seq->max-seq->min;//Maximum relative value    intsteps = Charset_len*rows;//Final number of rows    introw =0;Char*chars = Zmalloc (len);//len Column    int Loop=1;intOpt_fill = flags & Sparkline_fill;intOpt_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 + +) {structSample *s = &seq->samples[j+offset]; Double Relval = s->value-seq->min;//Relative value of each sample point            intStepif(opt_log) Relval =Log(relval+1); Step = (int) (relval*steps)/relmax;if(Step <0) Step =0;if(step >= steps) Step = steps-1;if(Row < rows) {intCharidx = step-((rows-row-1) *charset_len);Loop=1;if(Charidx >=0&& Charidx < Charset_len) {//Convert a number to an ASCII codeCHARS[J] = Opt_fill?                CHARSET_FILL[CHARIDX]: Charset[charidx]; }Else if(Opt_fill && charidx >= Charset_len) {Chars[j] =' | '; }            }Else{//For rows greater than row, do not process the sample, only the label is processed                /* ......  */}        }if(Loop) {row++;            Output = Sdscatlen (Output,chars,len); Output = Sdscatlen (output,"\ n",1); }} zfree (chars);returnOutput;}

This translates a series of sample values into a string of ASCII code.


The source code referenced in this article is all from the Redis3.0.7 version

Redis Learning Resources:
https://github.com/huangz1990/redis-3.0-annotated
Redis Design and Implementation (second edition)

Redis Learning Note (8)---Micro-line diagram Sparkline

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.