feat: add custom config for Bar, Line, Area

This commit is contained in:
Qiuxia Fan 2022-01-12 14:30:45 +08:00
parent ad1e500c54
commit 16085bb56f
10 changed files with 121 additions and 73 deletions

View File

@ -41,14 +41,20 @@ export interface StandardConfig {
min?: string; min?: string;
} }
export type GraphConfig = BarConfig | LineConfig; type GraphConfig = BarConfig | LineConfig;
interface BarConfig { export interface BarConfig {
type?: string; type?: string;
showBackground?: boolean; showBackground?: boolean;
barWidth?: number; barWidth?: number;
} }
interface LineConfig { export interface LineConfig extends AreaConfig {
type?: string; type?: string;
showBackground?: boolean; smooth?: boolean;
barWidth?: number; showSymbol?: boolean;
step?: boolean;
}
export interface AreaConfig {
type?: string;
opacity?: number;
} }

View File

@ -21,6 +21,7 @@ limitations under the License. -->
:is="states.chartType" :is="states.chartType"
:intervalTime="appStoreWithOut.intervalTime" :intervalTime="appStoreWithOut.intervalTime"
:data="states.source" :data="states.source"
:config="states.graph"
/> />
<div v-show="!states.chartType" class="no-data">{{ t("noData") }}</div> <div v-show="!states.chartType" class="no-data">{{ t("noData") }}</div>
</div> </div>
@ -66,7 +67,11 @@ limitations under the License. -->
</div> </div>
</el-collapse-item> </el-collapse-item>
<el-collapse-item :title="t('graphStyles')" name="3"> <el-collapse-item :title="t('graphStyles')" name="3">
<component :is="`${states.chartType}Config`" :config="states.graph" /> <component
:is="`${states.chartType}Config`"
:config="states.graph"
@update="updateGraphOptions"
/>
</el-collapse-item> </el-collapse-item>
<el-collapse-item :title="t('widgetOptions')" name="4"> <el-collapse-item :title="t('widgetOptions')" name="4">
<WidgetOptions <WidgetOptions
@ -212,6 +217,14 @@ export default defineComponent({
states.widget[param.label] = param.value; states.widget[param.label] = param.value;
} }
function updateGraphOptions(param: { label: string; value: unknown }) {
console.log(param);
states.graph = {
...states.graph,
...param,
};
}
async function queryMetrics() { async function queryMetrics() {
const json = await dashboardStore.fetchMetricValue( const json = await dashboardStore.fetchMetricValue(
dashboardStore.selectedGrid dashboardStore.selectedGrid
@ -224,7 +237,7 @@ export default defineComponent({
return; return;
} }
const metricVal = json.data.readMetricsValues.values.values.map( const metricVal = json.data.readMetricsValues.values.values.map(
(d: { value: number }) => d.value (d: { value: number }) => d.value + 1
); );
const m = states.metrics[0]; const m = states.metrics[0];
if (!m) { if (!m) {
@ -270,7 +283,7 @@ export default defineComponent({
metricOpts, metricOpts,
updateWidgetOptions, updateWidgetOptions,
configHeight, configHeight,
dashboardStore, updateGraphOptions,
applyConfig, applyConfig,
loading, loading,
}; };

View File

@ -14,32 +14,36 @@ See the License for the specific language governing permissions and
limitations under the License. --> limitations under the License. -->
<template> <template>
<div> <div>
<span class="label">Bar Width</span> <span class="label">Area Opacity</span>
<el-slider <el-slider
class="bar-width" class="bar-width"
v-model="barWidth" v-model="opacity"
show-input show-input
input-size="small" input-size="small"
:min="0.1"
:max="1"
:step="0.1"
@change="updateConfig({ opacity })"
/> />
</div> </div>
<div>
<span class="label">Show Background</span>
<el-switch v-model="showBackground" active-text="Yes" inactive-text="No" />
</div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { defineProps, ref } from "vue"; import { defineProps, ref, defineEmits } from "vue";
import type { PropType } from "vue"; import type { PropType } from "vue";
import { BarConfig } from "./types"; import { AreaConfig } from "@/types/dashboard";
const props = defineProps({ const props = defineProps({
config: { config: {
type: Object as PropType<BarConfig>, type: Object as PropType<AreaConfig>,
default: () => ({ showBackground: true, barWidth: 30 }), default: () => ({ opacity: 0.4 }),
}, },
}); });
const barWidth = ref(props.config.barWidth); const emits = defineEmits(["update"]);
const showBackground = ref(props.config.showBackground); const opacity = ref(props.config.opacity);
function updateConfig(param: { [key: string]: unknown }) {
emits("update", param);
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.bar-width { .bar-width {

View File

@ -20,17 +20,23 @@ limitations under the License. -->
v-model="barWidth" v-model="barWidth"
show-input show-input
input-size="small" input-size="small"
@change="changeConfig({ barWidth })"
/> />
</div> </div>
<div> <div>
<span class="label">Show Background</span> <span class="label">Show Background</span>
<el-switch v-model="showBackground" active-text="Yes" inactive-text="No" /> <el-switch
v-model="showBackground"
active-text="Yes"
inactive-text="No"
@change="changeConfig({ showBackground })"
/>
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { defineProps, ref } from "vue"; import { defineProps, ref, defineEmits } from "vue";
import type { PropType } from "vue"; import type { PropType } from "vue";
import { BarConfig } from "./types"; import { BarConfig } from "@/types/dashboard";
const props = defineProps({ const props = defineProps({
config: { config: {
@ -38,8 +44,13 @@ const props = defineProps({
default: () => ({ showBackground: true, barWidth: 30 }), default: () => ({ showBackground: true, barWidth: 30 }),
}, },
}); });
const barWidth = ref(props.config.barWidth); const emits = defineEmits(["update"]);
const showBackground = ref(props.config.showBackground); const barWidth = ref(props.config.barWidth || 30);
const showBackground = ref(props.config.showBackground || false);
function changeConfig(param: { [key: string]: unknown }) {
emits("update", param);
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.bar-width { .bar-width {

View File

@ -14,32 +14,52 @@ See the License for the specific language governing permissions and
limitations under the License. --> limitations under the License. -->
<template> <template>
<div> <div>
<span class="label">Bar Width</span> <span class="label">Smooth</span>
<el-slider <el-switch
class="bar-width" v-model="smooth"
v-model="barWidth" active-text="Yes"
show-input inactive-text="No"
input-size="small" @change="updateConfig({ smooth })"
/> />
</div> </div>
<div> <div>
<span class="label">Show Background</span> <span class="label">Show Symbol</span>
<el-switch v-model="showBackground" active-text="Yes" inactive-text="No" /> <el-switch
v-model="showSymbol"
active-text="Yes"
inactive-text="No"
@change="updateConfig({ showSymbol })"
/>
</div>
<div>
<span class="label">Step</span>
<el-switch
v-model="step"
active-text="Yes"
inactive-text="No"
@change="updateConfig({ step })"
/>
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { defineProps, ref } from "vue"; import { defineProps, ref, defineEmits } from "vue";
import type { PropType } from "vue"; import type { PropType } from "vue";
import { BarConfig } from "./types"; import { LineConfig } from "@/types/dashboard";
const props = defineProps({ const props = defineProps({
config: { config: {
type: Object as PropType<BarConfig>, type: Object as PropType<LineConfig>,
default: () => ({ showBackground: true, barWidth: 30 }), default: () => ({ showBackground: true, barWidth: 30 }),
}, },
}); });
const barWidth = ref(props.config.barWidth); const emits = defineEmits(["update"]);
const showBackground = ref(props.config.showBackground); const smooth = ref(props.config.smooth);
const showSymbol = ref(props.config.showSymbol);
const step = ref(props.config.step);
function updateConfig(param: { [key: string]: unknown }) {
emits("update", param);
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.bar-width { .bar-width {

View File

@ -1,20 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface BarConfig {
showBackground: boolean;
barWidth: number;
}

View File

@ -35,13 +35,13 @@ export const DefaultGraphConfig: { [key: string]: any } = {
}, },
Line: { Line: {
type: "Line", type: "Line",
showBackground: true, step: false,
barWidth: 30, smooth: false,
showSymbol: false,
}, },
Area: { Area: {
type: "Area", type: "Area",
showBackground: true, opacity: 0.4,
barWidth: 30,
}, },
}; };

View File

@ -14,12 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License. --> limitations under the License. -->
<template> <template>
<Line :data="data" :intervalTime="intervalTime" :type="'areaChart'" /> <Line :data="data" :intervalTime="intervalTime" :config="config" />
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { defineProps } from "vue"; import { defineProps } from "vue";
import type { PropType } from "vue"; import type { PropType } from "vue";
import Line from "./Line.vue"; import Line from "./Line.vue";
import { AreaConfig } from "@/types/dashboard";
defineProps({ defineProps({
data: { data: {
@ -27,5 +28,9 @@ defineProps({
default: () => ({}), default: () => ({}),
}, },
intervalTime: { type: Array as PropType<string[]>, default: () => [] }, intervalTime: { type: Array as PropType<string[]>, default: () => [] },
config: {
type: Object as PropType<AreaConfig>,
default: () => ({}),
},
}); });
</script> </script>

View File

@ -19,6 +19,7 @@ limitations under the License. -->
import { defineProps, computed } from "vue"; import { defineProps, computed } from "vue";
import type { PropType } from "vue"; import type { PropType } from "vue";
import { Event } from "@/types/events"; import { Event } from "@/types/events";
import { BarConfig } from "@/types/dashboard";
const props = defineProps({ const props = defineProps({
data: { data: {
@ -29,12 +30,8 @@ const props = defineProps({
theme: { type: String, default: "light" }, theme: { type: String, default: "light" },
itemEvents: { type: Array as PropType<Event[]>, default: () => [] }, itemEvents: { type: Array as PropType<Event[]>, default: () => [] },
config: { config: {
type: Object as PropType<{ type: Object as PropType<BarConfig>,
theme: string; default: () => ({}),
showBackground: boolean;
barWidth: number;
}>,
default: () => ({ theme: "light", showBackground: true, barWidth: 20 }),
}, },
}); });
const option = computed(() => getOption()); const option = computed(() => getOption());
@ -156,7 +153,7 @@ function getOption() {
left: 0, left: 0,
itemWidth: 12, itemWidth: 12,
textStyle: { textStyle: {
color: props.config.theme === "dark" ? "#fff" : "#333", color: "#333",
}, },
}, },
grid: { grid: {

View File

@ -19,16 +19,25 @@ limitations under the License. -->
import { defineProps, computed } from "vue"; import { defineProps, computed } from "vue";
import type { PropType } from "vue"; import type { PropType } from "vue";
import { Event } from "@/types/events"; import { Event } from "@/types/events";
import { LineConfig } from "@/types/dashboard";
const props = defineProps({ const props = defineProps({
data: { data: {
type: Object as PropType<{ [key: string]: number[] }>, type: Object as PropType<{ [key: string]: number[] }>,
default: () => ({}), default: () => ({}),
}, },
type: { type: String, default: "" },
intervalTime: { type: Array as PropType<string[]>, default: () => [] }, intervalTime: { type: Array as PropType<string[]>, default: () => [] },
theme: { type: String, default: "light" }, theme: { type: String, default: "light" },
itemEvents: { type: Array as PropType<Event[]>, default: () => [] }, itemEvents: { type: Array as PropType<Event[]>, default: () => [] },
config: {
type: Object as PropType<LineConfig>,
default: () => ({
step: false,
smooth: false,
showSymbol: false,
opacity: 0.4,
}),
},
}); });
const option = computed(() => getOption()); const option = computed(() => getOption());
function getOption() { function getOption() {
@ -68,6 +77,9 @@ function getOption() {
type: "line", type: "line",
symbol: "none", symbol: "none",
barMaxWidth: 10, barMaxWidth: 10,
step: props.config.step,
smooth: props.config.smooth,
showSymbol: true,
lineStyle: { lineStyle: {
width: 1.5, width: 1.5,
type: "solid", type: "solid",
@ -90,9 +102,9 @@ function getOption() {
} }
: undefined, : undefined,
}; };
if (props.type === "areaChart") { if (props.config.type === "Area") {
serie.areaStyle = { serie.areaStyle = {
opacity: 0.4, opacity: props.config.opacity || 0.4,
}; };
} }
return serie; return serie;