diff --git a/controllers/dataset.go b/controllers/dataset.go index 742c620..0768db8 100644 --- a/controllers/dataset.go +++ b/controllers/dataset.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/casbin/casbase/object" + "github.com/casbin/casbase/util" ) func (c *ApiController) GetGlobalDatasets() { @@ -27,8 +28,9 @@ func (c *ApiController) GetDataset() { func (c *ApiController) GetDatasetGraph() { id := c.Input().Get("id") + clusterNumber := util.ParseInt(c.Input().Get("clusterNumber")) - c.Data["json"] = object.GetDatasetGraph(id) + c.Data["json"] = object.GetDatasetGraph(id, clusterNumber) c.ServeJSON() } diff --git a/object/dataset_graph.go b/object/dataset_graph.go index 1be9fef..3707b1b 100644 --- a/object/dataset_graph.go +++ b/object/dataset_graph.go @@ -15,8 +15,10 @@ func init() { graphCache = map[string]*Graph{} } -func GetDatasetGraph(id string) *Graph { - g, ok := graphCache[id] +func GetDatasetGraph(id string, clusterNumber int) *Graph { + cacheId := fmt.Sprintf("%s|%d", id, clusterNumber) + + g, ok := graphCache[cacheId] if ok { return g } @@ -26,8 +28,10 @@ func GetDatasetGraph(id string) *Graph { return nil } + runKmeans(dataset.Vectors, clusterNumber) + g = generateGraph(dataset.Vectors) - graphCache[id] = g + graphCache[cacheId] = g return g } @@ -61,7 +65,7 @@ func getNodeColor(weight int) string { return fmt.Sprintf("rgb(%d,%d,%d)", myColor.R, myColor.G, myColor.B) } -var DistanceLimit = 11 +var DistanceLimit = 14 func generateGraph(vectors []*Vector) *Graph { vectors = refineVectors(vectors) diff --git a/object/kmeans.go b/object/kmeans.go index 7b33416..869d981 100644 --- a/object/kmeans.go +++ b/object/kmeans.go @@ -18,7 +18,7 @@ func fa2Str(floatArray []float64) string { return strings.Join(sData, "|") } -func runKmeans(vectors []*Vector) { +func runKmeans(vectors []*Vector, clusterNumber int) { vectorMap := map[string]*Vector{} var d clusters.Observations @@ -34,7 +34,7 @@ func runKmeans(vectors []*Vector) { } km := kmeans.New() - cs, err := km.Partition(d, 100) + cs, err := km.Partition(d, clusterNumber) if err != nil { panic(err) } @@ -61,7 +61,7 @@ func runKmeans(vectors []*Vector) { func updateDatasetVectorCategories(owner string, datasetName string) { dataset := getDataset(owner, datasetName) - runKmeans(dataset.Vectors) + runKmeans(dataset.Vectors, 100) UpdateDataset(dataset.GetId(), dataset) } diff --git a/web/src/Dataset.js b/web/src/Dataset.js index 55bb8a3..653195a 100644 --- a/web/src/Dataset.js +++ b/web/src/Dataset.js @@ -1,11 +1,13 @@ import React from "react"; -import {Card, Col, Empty, List, Row, Slider, Spin, Switch, Tooltip} from "antd"; +import {Button, Card, Col, Empty, InputNumber, List, Row, Select, Slider, Spin, Switch, Tooltip} from "antd"; import ForceGraph2D from 'react-force-graph-2d'; import ForceGraph3D from 'react-force-graph-3d'; import * as d3 from "d3-force"; import * as DatasetBackend from "./backend/DatasetBackend"; import i18next from "i18next"; +const { Option } = Select; + let fg = React.createRef(); class ForceGraph extends React.Component { @@ -43,6 +45,7 @@ class Dataset extends React.Component { // particlePercent: 100, strength: 20, distanceMax: 100, + clusterNumber: 100, selectedType: null, selectedId: null, selectedIds: [], @@ -54,11 +57,11 @@ class Dataset extends React.Component { } componentDidUpdate(prevProps, prevState, snapshot) { - fg.current.d3Force('collision', d3.forceCollide(15)); + fg.current?.d3Force('collision', d3.forceCollide(15)); } getDatasetGraph() { - DatasetBackend.getDatasetGraph("admin", this.state.datasetName) + DatasetBackend.getDatasetGraph("admin", this.state.datasetName, this.state.clusterNumber) .then((graph) => { this.setState({ graph: graph, @@ -247,6 +250,52 @@ class Dataset extends React.Component { + + 聚类参数 + + } type="inner"> + + + 算法: + + + + + + + + 聚类个数: + + + { + this.setState({ + clusterNumber: value, + }); + }} /> + + + + + + ) diff --git a/web/src/backend/DatasetBackend.js b/web/src/backend/DatasetBackend.js index 78304ad..b570cc6 100644 --- a/web/src/backend/DatasetBackend.js +++ b/web/src/backend/DatasetBackend.js @@ -21,8 +21,8 @@ export function getDataset(owner, name) { }).then(res => res.json()); } -export function getDatasetGraph(owner, name) { - return fetch(`${Setting.ServerUrl}/api/get-dataset-graph?id=${owner}/${encodeURIComponent(name)}`, { +export function getDatasetGraph(owner, name, clusterNumber) { + return fetch(`${Setting.ServerUrl}/api/get-dataset-graph?id=${owner}/${encodeURIComponent(name)}&clusterNumber=${clusterNumber}`, { method: "GET", credentials: "include" }).then(res => res.json());