github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/examples/components/linegraph_demo/linegraph/provider.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 linegraph
    16  
    17  import (
    18  	"reflect"
    19  
    20  	"github.com/erda-project/erda-infra/base/servicehub"
    21  	structure "github.com/erda-project/erda-infra/providers/component-protocol/components/commodel/data-structure"
    22  	"github.com/erda-project/erda-infra/providers/component-protocol/components/linegraph"
    23  	"github.com/erda-project/erda-infra/providers/component-protocol/components/linegraph/impl"
    24  	"github.com/erda-project/erda-infra/providers/component-protocol/cpregister/base"
    25  	"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
    26  	"github.com/erda-project/erda-infra/providers/component-protocol/protocol"
    27  )
    28  
    29  type provider struct {
    30  	impl.DefaultLineGraph
    31  }
    32  
    33  // RegisterInitializeOp .
    34  func (p *provider) RegisterInitializeOp() (opFunc cptype.OperationFunc) {
    35  	return func(sdk *cptype.SDK) cptype.IStdStructuredPtr {
    36  
    37  		// Demo case 1
    38  		d := linegraph.New("line graph demo")
    39  		d.SetXAxis("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    40  		d.SetYAxis("Dimension", 1, 2, 3, 4, 5, 6, 7)
    41  		d.SetXOptions(&linegraph.Options{
    42  			Structure: &structure.DataStructure{Type: structure.String},
    43  		})
    44  		d.SetYOptions([]*linegraph.Options{
    45  			{Dimension: "Dimension", Structure: &structure.DataStructure{Type: structure.Number}},
    46  			{Dimension: "Dimension2", Structure: &structure.DataStructure{Type: structure.Number}},
    47  		}...)
    48  		d.SetYAxis("Dimension2", 7, 6, 5, 4, 3, 2, 1)
    49  
    50  		// Demo case 2
    51  		d = linegraph.NewDataBuilder().
    52  			WithTitle("line graph demo").
    53  			WithXAxis("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday").
    54  			WithXOptions(linegraph.NewOptionsBuilder().WithType(structure.String).Build()).
    55  			WithYAxis("Dimension", 1, 2, 3, 4, 5, 6, 7).
    56  			WithYAxis("Dimension2", 7, 6, 5, 4, 3, 2, 1).
    57  			WithYOptions([]*linegraph.Options{
    58  				linegraph.NewOptionsBuilder().WithDimension("Dimension").WithType(structure.Number).Build(),
    59  				linegraph.NewOptionsBuilder().WithDimension("Dimension2").WithType(structure.Number).Build(),
    60  			}...).Build()
    61  
    62  		p.StdDataPtr = d
    63  		return nil
    64  	}
    65  }
    66  
    67  // RegisterRenderingOp .
    68  func (p *provider) RegisterRenderingOp() (opFunc cptype.OperationFunc) {
    69  	return p.RegisterInitializeOp()
    70  }
    71  
    72  // Init .
    73  func (p *provider) Init(ctx servicehub.Context) error {
    74  	p.DefaultLineGraph = impl.DefaultLineGraph{}
    75  	v := reflect.ValueOf(p)
    76  	v.Elem().FieldByName("Impl").Set(v)
    77  	compName := "linegraph"
    78  	if ctx.Label() != "" {
    79  		compName = ctx.Label()
    80  	}
    81  	protocol.MustRegisterComponent(&protocol.CompRenderSpec{
    82  		Scenario: "linegraph-demo",
    83  		CompName: compName,
    84  		Creator:  func() cptype.IComponent { return p },
    85  	})
    86  	return nil
    87  }
    88  
    89  // Provide .
    90  func (p *provider) Provide(ctx servicehub.DependencyContext, args ...interface{}) interface{} {
    91  	return p
    92  }
    93  
    94  func init() {
    95  	base.InitProviderWithCreator("linegraph-demo", "linegraph", func() servicehub.Provider { return &provider{} })
    96  }