github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/components/complexgraph/model.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package complexgraph
    16  
    17  import structure "github.com/erda-project/erda-infra/providers/component-protocol/components/commodel/data-structure"
    18  
    19  // Y axis positions
    20  const (
    21  	Right Position = "right"
    22  	Left  Position = "left"
    23  )
    24  
    25  // X axis positions
    26  const (
    27  	Top    Position = "top"
    28  	Bottom Position = "bottom"
    29  )
    30  
    31  // Define type
    32  const (
    33  	Value    Type = "value"
    34  	Category Type = "category"
    35  	Bar      Type = "bar"
    36  	Line     Type = "line"
    37  )
    38  
    39  // Below is standard struct for graph related.
    40  type (
    41  	// Data includes List.
    42  	Data struct {
    43  		Title      string   `json:"title"`
    44  		Dimensions []string `json:"dimensions"`
    45  		XAxis      []*Axis  `json:"xAxis"`
    46  		YAxis      []*Axis  `json:"yAxis"`
    47  		Series     []*Sere  `json:"series"`
    48  		Inverse    bool     `json:"inverse"`
    49  	}
    50  
    51  	//Axis .
    52  	Axis struct {
    53  		Type          Type                     `json:"type"`
    54  		Name          string                   `json:"name,omitempty"`
    55  		Dimensions    []string                 `json:"dimensions,omitempty"`
    56  		Position      Position                 `json:"position,omitempty"`
    57  		Data          []interface{}            `json:"data,omitempty"`
    58  		DataStructure *structure.DataStructure `json:"structure"`
    59  	}
    60  
    61  	//Sere .
    62  	Sere struct {
    63  		Name      string        `json:"name"`
    64  		Dimension string        `json:"dimension"`
    65  		Type      Type          `json:"type"`
    66  		Data      []interface{} `json:"data"`
    67  	}
    68  
    69  	//Type defined graph type
    70  	Type string
    71  
    72  	//Position defined axis position
    73  	Position string
    74  
    75  	// DataBuilder .
    76  	DataBuilder struct {
    77  		data *Data
    78  	}
    79  
    80  	// AxisBuilder .
    81  	AxisBuilder struct {
    82  		data *Axis
    83  	}
    84  
    85  	// SereBuilder .
    86  	SereBuilder struct {
    87  		data *Sere
    88  	}
    89  )
    90  
    91  // NewSereBuilder .
    92  func NewSereBuilder() *SereBuilder {
    93  	return &SereBuilder{data: &Sere{}}
    94  }
    95  
    96  // Build .
    97  func (d *SereBuilder) Build() *Sere {
    98  	return d.data
    99  }
   100  
   101  // WithName .
   102  func (d *SereBuilder) WithName(name string) *SereBuilder {
   103  	d.data.Name = name
   104  	return d
   105  }
   106  
   107  // WithDimension .
   108  func (d *SereBuilder) WithDimension(dimension string) *SereBuilder {
   109  	d.data.Dimension = dimension
   110  	return d
   111  }
   112  
   113  // WithType .
   114  func (d *SereBuilder) WithType(typ Type) *SereBuilder {
   115  	d.data.Type = typ
   116  	return d
   117  }
   118  
   119  // WithData .
   120  func (d *SereBuilder) WithData(data ...interface{}) *SereBuilder {
   121  	for _, datum := range data {
   122  		d.data.Data = append(d.data.Data, datum)
   123  	}
   124  	return d
   125  }
   126  
   127  // NewAxisBuilder .
   128  func NewAxisBuilder() *AxisBuilder {
   129  	return &AxisBuilder{data: &Axis{DataStructure: &structure.DataStructure{}}}
   130  }
   131  
   132  // Build .
   133  func (d *AxisBuilder) Build() *Axis {
   134  	return d.data
   135  }
   136  
   137  // WithName .
   138  func (d *AxisBuilder) WithName(name string) *AxisBuilder {
   139  	d.data.Name = name
   140  	return d
   141  }
   142  
   143  // WithDimensions .
   144  func (d *AxisBuilder) WithDimensions(dimensions ...string) *AxisBuilder {
   145  	for _, dimension := range dimensions {
   146  		d.data.Dimensions = append(d.data.Dimensions, dimension)
   147  	}
   148  	return d
   149  }
   150  
   151  // WithPosition .
   152  func (d *AxisBuilder) WithPosition(position Position) *AxisBuilder {
   153  	d.data.Position = position
   154  	return d
   155  }
   156  
   157  // WithData .
   158  func (d *AxisBuilder) WithData(data ...interface{}) *AxisBuilder {
   159  	for _, datum := range data {
   160  		d.data.Data = append(d.data.Data, datum)
   161  	}
   162  	return d
   163  }
   164  
   165  // WithDataStructure .
   166  func (d *AxisBuilder) WithDataStructure(typ structure.Type, precision structure.Precision, enable bool) *AxisBuilder {
   167  	d.data.DataStructure.Type = typ
   168  	d.data.DataStructure.Precision = precision
   169  	d.data.DataStructure.Enable = enable
   170  	return d
   171  }
   172  
   173  // WithType .
   174  func (d *AxisBuilder) WithType(typ Type) *AxisBuilder {
   175  	d.data.Type = typ
   176  	return d
   177  }
   178  
   179  // NewDataBuilder .
   180  func NewDataBuilder() *DataBuilder {
   181  	return &DataBuilder{data: &Data{}}
   182  }
   183  
   184  // Build .
   185  func (d *DataBuilder) Build() *Data {
   186  	return d.data
   187  }
   188  
   189  // WithTitle .
   190  func (d *DataBuilder) WithTitle(title string) *DataBuilder {
   191  	d.data.Title = title
   192  	return d
   193  }
   194  
   195  // WithDimensions .
   196  func (d *DataBuilder) WithDimensions(dimensions ...string) *DataBuilder {
   197  	for _, dimension := range dimensions {
   198  		d.data.Dimensions = append(d.data.Dimensions, dimension)
   199  	}
   200  	return d
   201  }
   202  
   203  // EnableInverse .
   204  func (d *DataBuilder) EnableInverse() *DataBuilder {
   205  	d.data.Inverse = true
   206  	return d
   207  }
   208  
   209  // WithXAxis .
   210  func (d *DataBuilder) WithXAxis(xAxis ...*Axis) *DataBuilder {
   211  	for _, axis := range xAxis {
   212  		d.data.XAxis = append(d.data.XAxis, axis)
   213  	}
   214  	return d
   215  }
   216  
   217  // WithYAxis .
   218  func (d *DataBuilder) WithYAxis(yAxis ...*Axis) *DataBuilder {
   219  	for _, axis := range yAxis {
   220  		d.data.YAxis = append(d.data.YAxis, axis)
   221  	}
   222  	return d
   223  }
   224  
   225  // WithSeries .
   226  func (d *DataBuilder) WithSeries(series ...*Sere) *DataBuilder {
   227  	for _, sere := range series {
   228  		d.data.Series = append(d.data.Series, sere)
   229  	}
   230  	return d
   231  }